C # "Object Equality comparison"

Source: Internet
Author: User
Tags instance method

The object equality comparison mechanism is different for variables of reference types and value types, and the following describes equality comparisons for reference types and value types, respectively.

Let's start with a partial definition of System.Object:

 public class Object {//Summary://Determines whether the specified System.Object equals the current System.Object.        Parameters://obj: System.Object compared to the current System.Object.        Returns the result: True if the specified System.Object equals the current System.Object, or false otherwise.        Public virtual bool Equals (object obj);        Summary://Determines whether the specified System.Object instance is considered equal.        Parameters://OBJB: The second System.Object to compare.        Obja: The first System.Object to compare.         Returns the result://True if Obja is the same instance as OBJB or if both are null references or if obja.equals (OBJB) returns true; otherwise//False.        public static bool Equals (object Obja, Object OBJB);        Summary://Determines whether the specified System.Object instance is the same instance.        Parameters://OBJB: The second System.Object to compare.        Obja: The first System.Object to compare.        Returns the result://True if Obja is the same instance as OBJB, or False if both are null references.    public static bool ReferenceEquals (object Obja, Object OBJB); }}

Three methods for equality of comparisons are defined in System.Object. There is also an operator (= =) that can compare references or compare values.

Here is a detailed introduction to these four methods:

1. Equality Comparison of reference types

Method 1:public static bool ReferenceEquals (object Obja, Object OBJB);

Description: ReferenceEquals () is a static method that tests whether two references point to the same instance of the class, that is, whether the address stored in the two variable is the same address.

Example:

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        SomeClass SC1 = new SomeClass ();        SomeClass SC2 = new SomeClass ();        Output false        Response.Write (ReferenceEquals (SC1, SC2) + "<br/>");        Output false        Response.Write (referenceequals (SC1, NULL) + "<br/>");        Output false        Response.Write (ReferenceEquals (NULL, SC1) + "<br/>");        Output true        Response.Write (referenceequals (NULL, NULL) + "<br/>");}    } public class someclass{}

Method 2:public Virtual bool Equals (object obj);

Description: This method also compares references, but because the method is virtual, you can override it in your own class and compare objects by value.

Example:

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        SomeClass SC1 = new SomeClass (ten);        SomeClass SC2 = new SomeClass (one);        Output false        Response.Write (SC1. Equals (SC2) + "<br/>");        Output false        Response.Write (SC1. Equals (NULL) + "<br/>");        Output true        Response.Write (SC1. Equals (SC1) + "<br/>");}    } public class someclass{    private int _age;    Public SomeClass (Int.)    {        this._age = age;    }    public override bool Equals (object obj)    {        SomeClass sc = obj as SomeClass;        if (sc = = null)        {            return false;        }        return this._age = = Sc._age;    }}

Method 3:public static bool Equals (object Obja, Object OBJB);

Description: The static version of equals has the same effect as the version of the virtual instance, with the difference being that the static version has two parameters. and compare them. This method can handle a condition where one of the two objects is null, so if an object is null, an exception is thrown.

Note: Returns true if two objects are null.

Returns False if only one of the objects is null.

If none of the two objects are null, the abstract Equals method is called.

This shows that if you are comparing two objects, overriding the virtual Equals method is equivalent to overriding the static Equals method.

Example:

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        SomeClass SC1 = new SomeClass (ten);        SomeClass SC2 = new SomeClass (one);        Output true        Response.Write (object. Equals (null, NULL) + "<br/>");        Output false        Response.Write (object. Equals (SC1, NULL) + "<br/>");        Output false        Response.Write (object. Equals (SC1, SC2) + "<br/>");}    } public class someclass{    private int _age;    Public SomeClass (Int.)    {        this._age = age;    }    public override bool Equals (object obj)    {        SomeClass sc = obj as SomeClass;        return this._age = = Sc._age;    }}
Comparison operator: = =
Description: This operator is between a strict value comparison and a strict reference comparison.
Example:
public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        SomeClass SC1 = new SomeClass ();        SomeClass SC2 = new SomeClass ();        Output false        Response.Write (SC1 = = SC2);}    } public class someclass{    }

2. Value type

When comparing values, ReferenceEquals is still used to compare references, as in the following example:

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        //Output False        Response.Write (referenceequals);    }}

The reason for the output false is a reference to the ReferenceEquals comparison, where a boxing operation was performed for two value types of 1 and 1, resulting in the fact that two parameters are actually different references.

In the case of a value comparison, Microsoft has overloaded the instance method equals in the System.ValueType class, so:

Equals is the value of the comparison:

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        int i = 1;        Output true        Response.Write (i.equals (1));}    }

For static equals. The virtual Equals method is actually called.

public partial class equals_default:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {        //Output True        Response.Write (object. Equals (n));}    }

Summary:

ReferenceEquals is used to compare references, and returns True if two instances point to the same object, otherwise false.

The static Equals method primarily calls the virtual Equals method when comparing non-empty objects and values. The main view is how the virtual Equals method is written when overridden.

C # "Object Equality comparison"

Related Article

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.