C # exploring equality judgment,

Source: Internet
Author: User

C # exploring equality judgment,

C # method for Equality judgment:

// Determine whether the specified System. Object is equal to the current System. Object
Public virtual bool Equals (object obj) {return RuntimeHelpers. equals (this, obj);} // determine whether to set the specified System. the Object instance is treated as equal public static bool Equals (object objA, object objB) {return (objA = objB) | (objA! = Null) & (objB! = Null) & objA. equals (objB);} // determine the specified System. whether the Object instance is the same instance public static bool ReferenceEquals (object objA, object objB) {return (objA = objB );}

= And! = Operator comparison content

**************************************** **************************************** **************************************

Using System; using System. collections; using System. collections. generic; using System. linq; using System. text; namespace ValueTypeDemo {// Shoes class {private string name; // product Name private int price; // unit price public string name {get {return name ;} set {name = value ;}} public int Price {get {return price;} set {price = value ;}}} class Program {static void Main (string [] args) {Shoes S1 = new Shoes (); Shoes S2 = new Shoes (); Shoes S3 = S2; int x = 5; int y = 8; int z = x; object A = 11; object B = A; object C = 11; // content comparison Console. writeLine (x = y); // false Console. writeLine (x = z); // true // compare Console by reference after packing. writeLine (A = B); // true Console. writeLine (A = C); // false // After packing, compare the values before packing to determine whether the Console is equal. writeLine (. equals (B); // true Console. writeLine (. equals (C); // true Console. writeLine (Equals (A, B); // true Console. writeLine (Equals (A, C); // true // force compare Console by reference. writeLine (ReferenceEquals (A, B); // true Console. writeLine (ReferenceEquals (A, C); // false Console. readKey ();}}}

 

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.