Equals vs GetHashCode when comparing objects
Should we override both Equals and GetHashCode properties when implementing a custom class instances comparison? In the following code I have a collection of classes. The class A is compared by the ID , the class B - by Code . using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<I> bars = new List<I>(); bars.Add(new A() { Id = 1, Code = "one A" }); bars.Add(new B() { Id = 1, Code = "one B" }); bars.Add(new A() { Id = 1, Code = "one A+" }); bars.Add(new B() { Id = 1,