static void Main() { var janet = new Person { FirstName = "Janet", LastName = "Jackson" }; Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet }; Person[] persons2 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet }; if (persons1 != persons2) Console.WriteLine("not the same reference"); if (!persons1.Equals(persons2)) Console.WriteLine("equals returns false - not the same reference"); if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer<Person>.Default)) { Console.WriteLine("the same content"); } var t1 = Tuple.Create<int, string>(1, "Stephanie"); var t2 = Tuple.Create<int, string>(1, "Stephanie"); if (t1 != t2) Console.WriteLine("not the same reference to the tuple"); if (t1.Equals(t2)) Console.WriteLine("equals returns true"); TupleComparer tc = new TupleComparer(); if ((t1 as IStructuralEquatable).Equals(t2, tc)) { Console.WriteLine("yes, using TubpleComparer"); } }
static void Main() { var janet = new Person { FirstName = "Janet", LastName = "Jackson" }; Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet }; Person[] persons2 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet }; WriteLine($"{persons1[0]} | {persons1[1]}"); WriteLine($"{persons2[0]} | {persons2[1]}"); if (persons1 != persons2) { WriteLine("not the same reference"); } if (!persons1.Equals(persons2)) { WriteLine("equals returns false - not the same reference"); } if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer <Person> .Default)) { WriteLine("the same content"); } var t1 = Tuple.Create <int, string>(1, "Stephanie"); var t2 = Tuple.Create <int, string>(1, "Stephanie"); if (t1 != t2) { WriteLine("not the same reference to the tuple"); } if (t1.Equals(t2)) { WriteLine("equals returns true"); } var tc = new TupleComparer(); if ((t1 as IStructuralEquatable).Equals(t2, tc)) { WriteLine("yes, using TupleComparer"); } if ((t1 as IStructuralEquatable).Equals(t2, new TupleComparer())) { WriteLine("equals using TupleComparer"); } }
static void Main() { Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" } }; Person[] persons2 = { new Person { FirstName = "Michael", LastName = "Jackson" } }; // 比较的是地址.. if (persons1 != persons2) { Console.WriteLine("not the same reference"); } // 数组没有实现 Equals 方法, 检测也会不相等 if (!persons1.Equals(persons2)) { Console.WriteLine("equals returns false - not the same reference"); } // 转换成 IStructuralEquatable 就可以实现比较 if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer <Person> .Default)) { Console.WriteLine("the same content"); } //------------------------------------------------ var t1 = Tuple.Create <int, string>(1, "Stephanie"); var t2 = Tuple.Create <int, string>(1, "Stephanie"); // 比较的是地址 if (t1 != t2) { Console.WriteLine("not the same reference to the tuple"); } // 相等, Tuple 有 Equal 方法 if (t1.Equals(t2)) { Console.WriteLine("equals returns true"); } // 使用自定义比较器 TupleComparer tc = new TupleComparer(); if ((t1 as IStructuralEquatable).Equals(t2, tc)) { Console.WriteLine("yes, using TubpleComparer"); } }