Home » ASP.NET » < write high quality code: 157 suggestions for imp...

< write high quality code: 157 suggestions for improving C# programs > reading notes 11-20

< write high quality code: 157 suggestions to improve the C# program > read notes 11-20, there is a friend in need can refer to.


recommendation 11: distinction = = and Equals

CLR divides equality into two categories:

1 value equality: the value of two variables equal.

2, reference equality: the two variable refers to the same object.

in memory
 but not all types are in accordance with itself, such as string is a special type of reference, but in FCL, string will be overloaded for more "values", rather than "the reference itself". For a custom type, if you want to achieve this value is not a reference comparison, you need to override the Equals method, such as Person, if IDCode is the same, we can say that they are the same person.

replication code

Class Person

{

Public, string, IDCode, {get; private set;}

Public Person (string idCode)

{

This.IDCode = idCode;


}
Public, override, bool, Equals (object, obj)

{

Return (obj as Person) IDCode =.IDCode;


}

}
Copy code

At this point, through the Equals to compare, then will be overloaded after the method of.


object, a = new, Person ("ABC");

Object, B = new, Person ("ABC");

Console.WriteLine (a = = b); //False

Console.WriteLine (a.Equals (b)); //True

Here, the difference between the author is still not plainly "= =" and "Equals", just say a word of advice: "for reference types, we must equal the definition of value, just to override the Equals method at the same time that government said reference equality"
.
At the same time, in order to make sure that there is a way to make sure the comparison is "reference equality", FCL provides the Object.ReferenceEquals method.


bool, equal=, object.ReferenceEquals (object, a, object, b);

Foreign affairs decided not to ask Google, the matter is not resolved by decompile, MSDN. In order to understand the difference between = = and Equals, I collected as follows:

1 = = is the operator, and Equals is the
method;
2, for a value type, string type, and Equals = = is to compare the value of Int content is equal, the use of ILSpy type anti compiler Equals observation; int type method of internal logic is "= =";


// int

[__DynamicallyInvokable, TargetedPatchingOptOut (Performance, critical, to, across, NGen, image, boundaries) inline]

Public bool Equals (int obj)

{

Return this = obj;


}
The string type is used to determine whether the referenced address is the same or the value is the same. Both have a condition that is considered equal. See the.

code for the string class

replication code

String / /

[__DynamicallyInvokable, ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail), TargetedPatchingOptOut (Performance, critical, to, across, NGen, image, boundaries) inline]

Public bool Equals (string value)

{

If (this = = null)

{

Throw, new, NullReferenceException ();


}
Return value! = null & & (object.ReferenceEquals (this, value) || (this.Length = = value.Length & & string.EqualsHelper (this, value)));


}

Copy code

3, for reference types, and Equals = are relatively stack memory address is equal, and custom types can be Override or Equals operator = = to rewrite that two objects are equal conditions, such as the Person class, I think that as long as the same as that of IDCard is equal, at this time can be overridden or overloaded.

See here, is not it a bit confused? = like Equals almost ah, in order to clarify this issue, I added the author Lu Minji QQ, the following is the chat record:


recommendation 12: Rewrite Equals and rewrite GetHashCode

Pit Dad! The last suggestion code was successfully compiled, but the compiler would be prompted by the friendship. Here, the author leads to another suggestion, "when,

!"

this is because if you override the Equals method without overriding the GetHashCode method, you might have a potential Bug.

when using the Dictionary class

replication code

Static Dictionary


Latest