Created
June 29, 2017 16:08
-
-
Save lucascebertin/297938c13d06989c2dfc8e5add7682e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| var first = new ComparisonTestDefault() { | |
| Id = 1, | |
| Name = "teste" | |
| }; | |
| var second = new ComparisonTestObjectMethodsOverrided(){ | |
| Id = 1, | |
| Name = "teste" | |
| }; | |
| //default output is the name of the type | |
| Console.WriteLine(first.ToString()); | |
| //output will be the id and the name | |
| Console.WriteLine(second.ToString()); | |
| //compare using hashcode/equals from objects | |
| var firstDict = new Dictionary<ComparisonTestDefault, int>(){ | |
| { new ComparisonTestDefault(){ Id = 1, Name = "1" }, 1 }, | |
| { new ComparisonTestDefault(){ Id = 2, Name = "2" }, 2 } | |
| }; | |
| var secondDict = new Dictionary<ComparisonTestObjectMethodsOverrided, int>(){ | |
| { new ComparisonTestObjectMethodsOverrided(){ Id = 1, Name = "1" }, 1 }, | |
| { new ComparisonTestObjectMethodsOverrided(){ Id = 2, Name = "2" }, 2 } | |
| }; | |
| var firstKeyFound = firstDict.ContainsKey(new ComparisonTestDefault(){ Id = 1, Name = "1" }); | |
| var secondKeyFound = secondDict.ContainsKey(new ComparisonTestObjectMethodsOverrided(){ Id = 1, Name = "1" }); | |
| //new object, can't be the inserted key (produces false) | |
| Console.WriteLine(firstKeyFound); | |
| //new object but with equals and gethashcode overwritten (produces true) | |
| Console.WriteLine(secondKeyFound); | |
| object a = null; | |
| object b = null; | |
| var c = new object(); | |
| var d = new object(); | |
| Console.WriteLine(ReferenceEquals(a,b)); | |
| Console.WriteLine(ReferenceEquals(c,d)); | |
| Console.WriteLine(ReferenceEquals(a,c)); | |
| Console.WriteLine(ReferenceEquals(a,d)); | |
| Console.WriteLine(ReferenceEquals(b,c)); | |
| Console.WriteLine(ReferenceEquals(b,d)); | |
| } | |
| } | |
| public class ComparisonTestDefault | |
| { | |
| public int Id {get;set;} | |
| public string Name {get;set;} | |
| } | |
| public class ComparisonTestObjectMethodsOverrided | |
| { | |
| public int Id {get;set;} | |
| public string Name {get;set;} | |
| public override string ToString(){ | |
| return $"{Id}: {Name}"; | |
| } | |
| public override int GetHashCode(){ | |
| unchecked { | |
| //hash base with a big prime number to generate a overflow mixing the bits and producing a better hash distribution | |
| var hash = (int)2166136261; | |
| //this will produce the overflow | |
| var prime = 16777619; | |
| //xoring the hash and the prime will follow the FNV hash function algorithm | |
| //https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function | |
| hash = (hash * prime) ^ Id.GetHashCode(); | |
| hash = (hash * prime) ^ Name.GetHashCode(); | |
| return hash; | |
| } | |
| } | |
| public override bool Equals(object value) | |
| { | |
| var obj = value as ComparisonTestObjectMethodsOverrided; | |
| //if the compared value is not "referentially" the same as null (a.Equals(null)) | |
| return !ReferenceEquals(null, obj) | |
| && string.Equals(Name, obj.Name) | |
| && Equals(Id, obj.Id); | |
| } | |
| } | |
| /* | |
| ComparisonTestDefault | |
| 1: teste | |
| False | |
| True | |
| True | |
| False | |
| False | |
| False | |
| False | |
| False | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment