"Effective C #": Distinguishing and understanding four-sentence functions

Source: Internet
Author: User
Tags comparison table

. Net has four of the functions of the sentence? Many people see this headline and will be skeptical about it. In fact,. Net provides referenceequals, static equals, a specific type of equals , and the = = operator, which is the four-sentence function. However, there is a slight relationship between the four functions, and changing the implementation of one of these functions will affect the result of the operation of other functions.

The first thing to say is that the two static functions of object.referenceequals and object.equals , for both of them, do not need to be rewritten, because they have done what they want to do.

For object.referenceequals This static function, the function situation is as follows:

public static bool ReferenceEquals (Object-left, object-right);

This function is to determine whether two reference type objects point to the same address. With this description, the scope of its use is determined, that is, only for reference type operations. Then for any value-type data operation, even if it is judged by itself, it will return false. This is mainly because when this function is called, the value type data is boxed, that is, for the following form.

int n = 10;

Object.referenceequals (n, N);

this is because for n This data is boxed two times, and the address after each packing is different, which causes object.referenceequals (n, N) The result is always false.

For the first sentence function, there is nothing to expand, because it has done well in itself.

For the second object.equals , this static function has the following form:

public static bool Equals (Object-left, object-right);

According to the analysis of the book, its approximate function code is as follows:

public static void Equals (Object-left, object-right)

{

Check Object Identity

if (left = = right)

return true;

Both null references handled above

if (left = = null) | | (right = = null))

return false;

return left. Equals (right);

}

It can be said thatobject.equals This function completes the operation of the sentence, it takes three steps, the first step is to depend on the type of the object = = the execution result of the operator; The second step is to determine if null , as well as the first step, needs to be based on the type of = = the execution result of the operator; the last step to use Equals to the type the execution result of the function. That is to say, the return result of this static function depends on the two judgments and other functions to be mentioned later. Whether the type provides the appropriate judgment function is an important factor to return the result of this function.

So for object.equals This static method, although the type that accepts the parameter also belongs to the reference type, but differs from the object.referenceequals function, for the following code, Can draw the right results.

int n = 10;

Debug.WriteLine (String. Format ("{0}", Object.Equals (n, N)));

Debug.WriteLine (String. Format ("{0}", Object.Equals (n, 10)));

This is because the specific type of the two-sentence function is used in this function, but as far as the function itself is concerned, the judgment is done, so there is no need to overload to add complex operations.

In order to better describe the remaining two functions, first explain the meaning of equivalence. The meaning of equivalence is reflexive, symmetric, and transitive.

The so-called reflexive, namely a = = a;

And symmetry, is a = = B, then b = = a;

Pass is a = = b,b = = C, then a = = C;

After understanding the meaning of equivalence, the equivalence rule is also satisfied in the realization of the function of the type's judgment.

For the two-sentence function that can be overloaded, the first one is to describe the Equals function of the type, which has the following approximate form:

public override bool Equals (object right);

So what do you do with a type of Equals , generally as follows:

public class KeyData

{

private int ndata;

public int Data

{

get{return ndata;}

set{ndata = value;}

}

public override bool Equals (object right)

{

Check NULL

if (right = = null)

return false;

Check reference equality

if (object. ReferenceEquals (this, right))

return true;

Check type

if (this. GetType ()! = right. GetType ())

return false;

Convert to current type

KeyData Rightaskeydata = right as KeyData;

Check Members Value

return this. Data = = Rightaskeydata.data;

}

}

A type check has been added as above, i.e.

if (this. GetType ()! = right. GetType ())

in this section, this is because the subclass object can be Into the base class object, which causes different types of objects to be sentenced and other operations, violating the equivalence relationship.

In addition to Equals for the type function, in fact, there is no restriction type to be a reference type, for value types can also overload this function, but I do not recommend, mainly Equals The parameter type of the function is immutable, that is, by this method, the value type is boxed, which is more efficient than the other.

For value types, I recommend the last one, the overloaded operator = = function, with the following approximate form:

public static BOOL operator = = (KeyData left, KeyData right);

For a value type, its approximate form should be as follows:

public struct KeyData

{

private int ndata;

public int Data

{

get{return ndata;}

set{ndata = value;}

}

public static BOOL operator = = (KeyData left, KeyData right)

{

return left. Data = = right. Data;

}

public static bool Operator! = (KeyData left, KeyData right)

{

return left. Data! = right. Data;

}

}

because = = action with! = operation to synchronize the definition, so in the definition = = when you overload a function, you also define the! = overloaded functions. This is also consistent with . Net in the operation of the award. So for the Last Judgment function, this overloaded operator's method is not suitable for reference types. This is the . Net Common phenomenon, to judge two reference types, do not use = =, but to use the Equals function of an object . So when writing your own type, keep this style.

Then for the above four kinds of judgment function, will produce the following similar comparison table.

The result of the operation depends

Scope of application

Suggestions

object.referenceequals

Two parameter objects are of the same reference

Do not use it to determine value type data

object.equals

unrestricted

Consider the effect of boxing operations on value type data

equals

unrestricted

Consider the effect of boxing operations on value type data

types of = = overloaded

Type overloaded functions

Unlimited

Do not overload this operator in a reference type

So when writing the type judgment function, what should pay attention to, give the following suggestions.

First of all, to determine whether the currently defined type has the meaning of judgment, etc.

Second, the definition of the type of the sentence function to meet the equivalent rules;

Finally, value types are best not overloaded to define Equals function, and the reference type is best not to overload the definition = = operator.

"Effective C #": Distinguishing and understanding four-sentence functions

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.