Strive to be updated every two days in the future. On weekdays, too Much temptation, double Hugh just play, progress a bit slow.
Next, the type of security, left a little tail--compare the equality of objects.
There are four ways to compare equality in C #: Except for the "= =" operator, System.Object defines 3 methods: the Referenceequal () method and the two Equals ();
1. The first is the "= =" Operator:
For a value type, "= =" Compares two values for equality, whereas for reference types, "= =" compares the reference addresses of two objects. Here is a special case, string string type, "= =" is the value of the comparison string rather than the reference address, because the system overrides the "= =" operator. About overloading of operators, which is said later.
2.ReferenceEqual () method
Referenceequal () is a static method used to compare whether two objects refer to the same instance of a class, that is, a reference to an address from the same memory. As a static method, it cannot be overridden. Returns true if two references are referenced from the same object instance, otherwise false.
SomeClass S1 = new SomeClass ();
SomeClass s2 = new SomeClass ();
SomeClass s3 = S1;
BOOL B1 = referenceequals (null, NULL);
BOOL B2 = referenceequals (S1, S2);
Console.WriteLine (B1); True
Console.WriteLine (B2); False, different instances are created and reference addresses are different
Console.WriteLine (ReferenceEquals (S1, S3)); TRUE,S1,32 reference address is the same
Console.WriteLine (ReferenceEquals (1, 1)); Flse, because it is not a comparison value, but rather a reference address
Note the point:
Because ReferenceEquals () is a comparison of reference types, when a comparison between value types is performed, the boxing (previous mentioned) action is taken, and the value type is converted to a reference type, so it returns false.
BOOL B=referenceequals (); will return a value of false
3. Virtual equal () method: Equals (Object)
Description: Determines whether the specified object is equal to the current object.
Because it is a virtual method, it can be overridden. Similar to the "= =" operator, which supports comparing value types and reference types, where string is compared as a value type
4. Static Equals () method: Equals (Object,object)
Description: Determines whether the specified object instance is considered equal.
As for the Equals method, the book is just a brief introduction, and the detailed answers can be consulted in the MSDN documentation: Http://msdn.microsoft.com/zh-cn/library/System.Object.Equals (v=vs.100). ASPX, which has a detailed description of the two methods of equals ().
In the next article, the overloaded operator.
Learn "C # Advanced Programming" 2--Comparison object equality