Skip to content

Instantly share code, notes, and snippets.

@teo-mateo
Last active September 17, 2020 22:15
Show Gist options
  • Select an option

  • Save teo-mateo/c7cf74207bd3abcd10813b6def0008fd to your computer and use it in GitHub Desktop.

Select an option

Save teo-mateo/c7cf74207bd3abcd10813b6def0008fd to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace LeMoma
{
public static class Program
{
public static void Test<TMoma>(this TMoma moma) where TMoma : IMoma
{
var dict = new Dictionary<IMoma, string>();
dict.Add(moma, "X");
Console.WriteLine("Can I find the moma KEY in the dictionary? " + dict.ContainsKey(moma));
var momaClone = JsonConvert.DeserializeObject<TMoma>(JsonConvert.SerializeObject(moma));
Console.WriteLine("Can I find the moma KEY in the dictionary using a cloned moma? " + dict.ContainsKey(momaClone));
moma.Ma++;
Console.WriteLine("After I update moma's value, can I still find it in the dictionary? " + dict.ContainsKey(moma));
}
static void Main(string[] args)
{
Console.WriteLine("\r\nTesting with value type---");
new ValueMoma() { Mo = "mo", Ma = 1 }.Test();
Console.WriteLine("\r\nTesting with reference type---");
new ReferenceMoma() { Mo = "mo", Ma = 1 }.Test();
}
public interface IMoma
{
string Mo { get; set; }
int Ma { get; set; }
}
struct ValueMoma : IMoma
{
public string Mo { get; set; }
public int Ma { get; set; }
}
class ReferenceMoma : IMoma
{
public string Mo { get; set; }
public int Ma { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment