usingSystem;usingSystem.Diagnostics;usingSystem.Text;usingSystem.Collections;usingSystem.Collections.Generic;classtest{Static voidPrintObjectobj) {Console.WriteLine (obj); } classCdefoveride//all classes in C # inherit from object by default, and there's no need to write inheritance or write, it's kinda weird . { Public Override BOOLEquals (ObjectObj//override Object. Equals (Object obj), and the system's string class does the same{print ("cdefoveride.equals"); return true; } } Static voidMain () {stringSA =New string(New Char[] {'h','e','L','L','o' }); stringSB =New string(New Char[] {'h','e','L','L','o' }); Print (SA. GetHashCode ()+","+sb. GetHashCode ()); Print (SA. Equals (SB));//true to call String.Equals (string)Print (sa = SB);//True,string's operator = = ObjectOA =sa; ObjectOB =SB; Print (OA. Equals (OB));//true, the polymorphic call, which is actually called string. Equals (object)Print (OA = = OB);//false ObjectOC =New Object(); ObjectOD =New Object(); Print (OC. Equals (OD)); //false, Object.Equals (object)Print (OC = = OD);//false//If there is no implementation override, for reference types, then the original Object.Equals () and = = Do not have any difference, they always get the same result//because the reference type is actually a pointer, = = comparison is the value of the pointer, that is, the address, equals is the address of the comparison. //The string class overrides = = and equals, implementing a comparison of string content rather than an address. ObjectO1 =Newcdefoveride (); ObjectO2 =Newcdefoveride (); Print (O1. Equals (O2)); //false, polymorphic invocation, Cdefoveride.equals (object) intIA = A; ShortISA = A; Print (IA. Equals (ISA)); //true, short can be converted to int, so polymorphic call Int32.equals (Int32 obj)Print (ISA. Equals (IA));//false, int cannot be directly converted to short, so polymorphic call Int16.equals (object obj) }}
c # = equals essence Understanding