In C #, there are two ways to judge equality, one is the traditional = = operation, and one is the Equals method provided by object. The difference between the two is:
The = = operator determines the value in the stack, and Equlas determines the value in the heap.
C # provides value types and reference types, value types are stored on the stack, so use = = to determine whether their values are equal, because the value type does not exist in the heap data, so the value type equals is also the judgment data. That is, for value types, = = is the same as equals to determine whether their values are equal.
For reference types, where the address of an object is stored in the stack, then = = Compares two addresses for equality, that is, whether they point to the same object, and the Equals function compares the data in the heap of two objects, that is, whether two reference types are references to the same object.
Ii. special type of string
Although a string type is a reference type, the assignment to a string object is done by value type.
For example:
String s1= "Hello"; String s2= "Hello";
When the S2 is initialized, it does not reopen the memory, but instead directly points its address to the content "Hello" of S1. This way, although the string type is a reference type, its = = operation is the same as the equals operation, and the values are equal.
Iii. relationship with GetHashCode ()
If the two objects equals equal, then their GetHashCode () must be equal, but conversely, if GetHashCode () is equal, then the Equals method of the two objects is not necessarily the same as the result of the comparison. (For best performance, the hash function generates random numbers for the object content, which means that objects with different content may generate the same number, but this probability is considered very small).
The following example illustrates:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication1{classpeople {Private stringname; Public stringName {Get{returnname;} Set{name =value;} } PublicPeople (stringname) { This. Name =name; } } classProgram {Static voidMain (string[] args) { stringA ="Hello"; stringb =New string(New Char[] {'h','e','L','L','o' }); Console.WriteLine (A==b); Console.WriteLine (A.equals (b)); Console.WriteLine ("\ n"); Int32 m=3; Int32 N=3; Console.WriteLine (n==m); Console.WriteLine (N.equals (m)); Console.WriteLine ("\ n"); Objectg =A; Objecth =b; Console.WriteLine (G==h); Console.WriteLine (G.equals (h)); Console.WriteLine (G.gethashcode ()+" "+H.gethashcode ()); Console.WriteLine ("\ n"); People P1=NewPeople ("Jimmy"); People P2=NewPeople ("Jimmy"); Console.WriteLine (P1==p2); Console.WriteLine (P1. Equals (p2)); Console.WriteLine (P1. GetHashCode ()+" "+P2. GetHashCode ()); Console.WriteLine ("\ n"); People P3=NewPeople ("Jimmy"); People P4=P3; Console.WriteLine (P3==p4); Console.WriteLine (P3. Equals (p4)); } }}
The results of the operation are as follows:
True True True False true -695839 -695839 False false 46104728 12289376 true true Press any key to continue ...
In C #, the characteristics of string types are:
(1) The basic data type;
(2) is a reference type;
(3) Read-only;
(4) string a= "1123";
String b= "1123";
Then A and B point to the same memory address;
But not 2 strings of equal value correspond to the same memory address;
(5) 2 String Type "= =" operation, first determine whether the memory address is the same, if the same, immediately return true, if the memory address is not the same, continue to determine whether the value is the same.
The following example illustrates:
Static voidMain (string[] args) { stringA =New string(New Char[] {'C'H', E'N', '1' }); stringb =New string(New Char[] {'C'H', E'N', '1' });Console.WriteLine (A = = B);//result is trueConsole.WriteLine (A.equals (b));//result is true Objectg =A; Objecth =b; Console.WriteLine (G= = h);//The result is falseConsole.WriteLine (G.equals (h));//result is truePerson P1=NewPerson ("CSW"); Person P2=NewPerson ("CSW"); Console.WriteLine (P1= = P2);//The result is falseConsole.WriteLine (P1. Equals (p2));//The result is falsePerson P3=NewPerson ("CSW"); Person P4=P3; Console.WriteLine (P3= = P4);//result is trueConsole.WriteLine (P3. Equals (p4));//result is trueConsole.ReadLine (); }
The difference between equal and = = in C #