C # programming (39)----------comparing the equality of objects

Source: Internet
Author: User
Tags type casting

Compare equality of objects

Mechanisms that need to understand the equality of objects are important for programming logical expressions , and also important for implementing operator overloading and type casting .

The mechanism for object equality differs , depending on whether the reference type or value type is being compared .

Comparing the equality of reference types

System.Object defines three different methods to compare the equality of objects : ReferenceEquals () and Equals () Two versions , plus the comparison operators , There are actually four ways to do equality comparisons .

1.ReferenceEquals () method

The following is the definition of the referenceequals () method

public static bool ReferenceEquals (object Obja, Object OBJB)

ReferenceEquals ()is a static method,Testing two references is enough to refer to the same instance of a class,Especially if two references contain the same address in memory.as a static method , He cannot rewrite it, so System.Object 's implementation code remains the same . If the two references provided refer to the same object instance , referenceequals () always returns true; Otherwise, it returns false. But it defaults to null equals null;

Case :

SomeClass x, y;

X=new SomeClass ();

Y=someclass ();

BOOL B1=referenceequals (Null,null)// return True

BOOL B2=referenceequals (NULL,X)// return False

BOOL B3=referenceequals (x, y)// return False , because x and y point to different objects

2. The virtual Equals () method

The following is the definition of the Equals () method :

Public virtual bool Equals (object obj)

Returns a value that indicates whether the current object is equal to the current object.

the default implementation of Equals only supports reference equality , but derived classes can override this method to always eat values equal .

In the following list , X, y, z represent a reference that is not empty :

(1) x.equals (x) returns true In addition to the design of floating-point types

(2) x.equals (y) returns the same value as Y.equals (x)

(3) If both x and y are NaN, x.equals (y) returns true.

(4) If and only if x.equals (z) returns true , (x.equals (y) &&y.equals (z)) returns the same value

(5) camera calls to X.equals (y) return the same value as long as the objects referenced by x and Y are not modified

(6) X.equals (null) return false

Case :

public class Sample

{

public void Method ()

{

Object obj1 = new Object ();

Object obj2 = new Object ();

Console.WriteLine (obj1. Equals (OBJ2));// return False

Obj2 = obj1;

Console.WriteLine (obj1. Equals (OBJ2));// return True

Console.readkey ();

}

}

3. static Equals () method

The following is the definition of the static Equals () method

public static bool Equals (object Obja, Object OBJB)

If obja is the same instance as OBJB, or if both are null references , or if obja.equals (OBJB) returns true; true, otherwise false.

Equality Comparison of value types

When comparing equality of value types , with the same rules as the reference type :referenceequals () used to compare references ,equals () For comparison values The biggest difference is , Value types need to be boxed to convert them to references , to be able to execute methods on them

ReferenceEquals () always returns False when the value type is applied, because the value type needs to be boxed into the object in order to call this method . Even if you use the following code :

int v = 10;

BOOL B = ReferenceEquals (V, v);

Console.WriteLine (b);//b returns false

Because when each parameter is converted , V sleeps individually and is boxed .

C # programming (39)----------comparing the equality of objects

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.