c # = equals essence Understanding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.