static void Main(string[] args) { Person p = new Person(); Console.WriteLine("ToString(): {0}", p.ToString()); Console.WriteLine("GetHashCode(): {0}", p.GetHashCode()); Console.WriteLine("GetType(): {0}", p.GetType()); Person p2 = p; Object o = p2; if (o.Equals(p) && p2.Equals(o)) { Console.WriteLine("SAME INSTANCE!"); } Console.ReadKey(); }
static void Main(string[] args) { Person p = new Person("Scott", "Smith", 30); Console.WriteLine("ToString(): {0}", p.ToString()); Console.WriteLine("GetHashCode(): {0}", p.GetHashCode()); Console.WriteLine("GetType(): {0}", p.GetType()); Person p2 = new Person("Scott", "Smith", 30); if (p.Equals(p2)) { Console.WriteLine("Same State"); } else { Console.WriteLine("Different State"); } if (Object.Equals(p, p2)) { Console.WriteLine("Same State"); } else { Console.WriteLine("Different State"); } if (p.GetHashCode() == p2.GetHashCode()) { Console.WriteLine("Same Hash Codes"); } else { Console.WriteLine("Different Hash Codes"); } p2.Age = 45; if (p.Equals(p2)) { Console.WriteLine("Same State"); } else { Console.WriteLine("Different State"); } if (p.GetHashCode() == p2.GetHashCode()) { Console.WriteLine("Same Hash Codes"); } else { Console.WriteLine("Different Hash Codes"); } Person p3 = p2; if (Object.ReferenceEquals(p3, p2)) { Console.WriteLine("Same Reference"); } else { Console.WriteLine("Different Reference"); } Console.ReadKey(); }