public void TestInterfaceProxyEquals() { Duck duck1 = new Duck(); Duck duck2 = new Duck(); IInterface proxy1a = DuckTyping.Cast <IInterface>(duck1); IInterface proxy1b = DuckTyping.Cast <IInterface>(duck1); IInterface proxy2 = DuckTyping.Cast <IInterface>(duck2); Assert.IsTrue(proxy1a.Equals(proxy1b), "Interface proxy does not properly forward calls to the Equals method."); Assert.IsTrue(!proxy1a.Equals(proxy2), "Interface proxy overrides the Equals method improperly."); Assert.AreEqual(proxy1a.GetHashCode(), duck1.GetHashCode(), "Interface proxy does not properly forward calls to the GetHashCode method."); }
public override int GetHashCode() { unchecked { int hashCode = Number; hashCode = (hashCode * 397) ^ (Text != null ? Text.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Nested != null ? Nested.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (List != null ? List.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Array != null ? Array.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Dictionary != null ? Dictionary.GetHashCode() : 0); hashCode = (hashCode * 397) ^ IgnoreInt; hashCode = (hashCode * 397) ^ PrivateInt; hashCode = (hashCode * 397) ^ (Polymorf1 != null ? Polymorf1.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Polymorf2 != null ? Polymorf2.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Custom != null ? Custom.GetHashCode() : 0); hashCode = (hashCode * 397) ^ Struct.GetHashCode(); return(hashCode); } }
static void Main(string[] args) { MyStruct instance; Console.WriteLine(instance.GetHashCode()); //HashCod'ы ОДИНАКОВЫ instance.Method(); //Упаковка Boxing IInterface boxed = instance; Console.WriteLine(boxed.GetHashCode()); //HashCod'ы ОДИНАКОВЫ boxed.Method(); //Распаковка UnBoxing MyStruct unBoxed = (MyStruct)boxed; Console.WriteLine(unBoxed.GetHashCode()); //HashCod'ы ОДИНАКОВЫ unBoxed.Method(); //Delay Console.ReadKey(); }