Analysis of "=" and Equals in C,
1. Is there a correlation between "=" and "Equals?
Most netizens of "=" and Equals summarize the following:
For example, this article takes the example in this article as an example.
public class Person { public Person(string name) { this.Name = name; } public string Name { get; set; } } static void Main(string[] args) { string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); Console.WriteLine(a == b); //true Console.WriteLine(a.Equals(b)); //true object g = a; object h = b; Console.WriteLine(g == h); //false Console.WriteLine(g.Equals(h)); //true Person p1 = new Person("jia"); Person p2 = new Person("jia"); Console.WriteLine(p1 == p2); //false Console.WriteLine(p1.Equals(p2)); //false Person p3 = new Person("jia"); Person p4 = p3; Console.WriteLine(p3 == p4); //true Console.WriteLine(p3.Equals(p4)); //true Console.ReadKey(); }
If the above conclusion is correct, "=" is to compare two variables with equal values, then the following code should not be True.
Console.WriteLine(a == b); //true
Obviously, the above two string variables a and B point to two different objects, that is, their memory addresses stored in the stack space are also different. But are they equal?
2. What is Operator overload?
Operator Overloading means to redefine existing operators and assign them another function to adapt to different data types. For example, in the "+" operator
When all edges are numeric variables, the "+" operator indicates the mathematical meaning of "+. If either side of the "+" operator is of the string type, the "+" operator indicates the connection.
The meaning of the string. There are many heavy-duty instances for such operators. Is there a gross relationship with the topic of this article? What I want to say is that the above String variables a and B are caused by the String class.
The operator "=" is overloaded. Check the following source code:
public static bool operator == (String a, String b) { return String.Equals(a, b); } public static bool operator != (String a, String b) { return !String.Equals(a, b); }
Obviously, the "=" operator is really overloaded in the String class, and there are more than "=" and "! = "Oh. In addition, The Equals method in the String class is directly called within the overload operator method,
The source code is as follows:
public static bool Equals(String a, String b) { if ((Object)a==(Object)b) { return true; } if ((Object)a==null || (Object)b==null) { return false; } if (a.Length != b.Length) return false; return EqualsHelper(a, b); }
It is possible that the "=" operator does not necessarily compare whether the values stored in the two variables are equal. This depends on whether the current operator is overloaded in the current type.
3. Rewrite Equals
Or the above example:
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); Console.WriteLine(a == b); //true Console.WriteLine(a.Equals(b)); //true
We can see from the above: a and B are two different objects. However, if Equals is True, the above conclusion is not True: "Equals is to compare whether two variables point to the same object. Cause
Check the Equals method in the String class:
public override bool Equals(Object obj)
{ if (this == null) //this is necessary to guard against reverse-pinvokes and throw new NullReferenceException(); //other callers who do not use the callvirt instruction String str = obj as String; if (str == null) return false; if (Object.ReferenceEquals(this, obj)) return true; if (this.Length != str.Length) return false; return EqualsHelper(this, str); } public bool Equals(String value)
{ if (this == null) //this is necessary to guard against reverse-pinvokes and throw new NullReferenceException(); //other callers who do not use the callvirt instruction if (value == null) return false; if (Object.ReferenceEquals(this, value)) return true; if (this.Length != value.Length) return false; return EqualsHelper(this, value); }
We can see from the above that the String class not only overrides the Equals in the Object but also has its own Equals method, but the implementation code is almost the same. Comparison type, memory address,
To obtain the final result. Therefore, Equals does not necessarily mean a single comparison of whether the referenced addresses are the same, but we can also rewrite and customize them. But rewrite
You also need to note that if you need to use HashMap, HashSet, and Hashtable, you also need to override GetHashCode ().
4. Why is Equals required when "=" is available?
China has a saying: "The existence of anything must have the truth and value of his existence." The same is true for "=" and "Equals. The most basic implementation of "=" in the reference type is to decompare
If the two objects have the same memory address, if they are the same, otherwise, they may not. This implementation is obviously from the hardware perspective. If two objects are equal, they are the same object,
The addresses in the memory must be equal.But in many cases, "behavior (method)" depends on how we observe the world.For example, for the String type, we declare a character.
The string is more concerned with the actual value of the string, rather than whether the two objects are created once or twice in the memory (that is, whether the memory address is equal), as long as they have
The actual values are equal, so we think they are equal, which is understood from the Life business logic rather than from the machine perspective. Of course, the same string is declared above
Whether the variable is created once or twice I want to: "constant pool (or string detention pool)" has given us the best solution.
5. What is the relationship between "=" and "Equals?
"=" Operators and Equals are actually complementary.Because: "=" the main implementation form of operators is implemented from the "Computer perspective (or hardware perspective,
Equals is implemented based on common business scenarios or specific business scenarios. There is no necessary link between the two. It is only necessary to select different methods based on your business needs.
Therefore, the Equals in the Object is Visual, and many classes have rewritten it, And it truly meets the specific behavior required in the current type, that is, polymorphism. So it is not difficult to explain the above:
object g = a; object h = b; Console.WriteLine(g == h); //false Console.WriteLine(g.Equals(h)); //true
Because the Object does not implement the overload operator "=", the current "=" comparison method is to compare whether the memory addresses of the two variables in the stack space are the same. Equals is
Calls Equals in the String class because the g variable actually points to a String Object during running, and the current Object type is only the rows of Visual studio and compiler.
Is, that is, whether it is a polymorphism.
Finally, everything has its rules: "=" and Equals are no exception. For details, click: Jump to MSDN.