Introduction to this article:
Equals, ==, in C# ReferenceEquals can be used to determine whether the individuals of two objects are equal. For the same basic value type, the comparison results of == and Equals() are the same; since ReferenceEquals() determines whether the references of two objects are equal, for values Type, because a boxing operation must be performed before each judgment, that is, a temporary object is generated every time, so false will always be returned.
1. == operator
1. Static equality symbol, corresponding to the existing !=. This symbol is an overloadable binary operator that can be used to compare whether two objects are equal.
2. It will automatically perform necessary type conversion as needed, and return true or false depending on whether the values of the two objects are equal.
3. For reference objects, compare their references (except for string reference types, string is a comparison value)
4. For value types, compare their values
5. Some built-in reference types overload the == symbol, such as string Just overload == so that it compares not the references of the two strings, but whether the two string literals are equal.
6. For example:
int i = 5; int j = 5; Console.WriteLine(i == j);//值类型比较代数值 输出True int m = 6; double n = 6.0; Console.WriteLine(m == n);//类型自动转换并比较数值 输出True object obj1 = new object(); object obj2 = new object(); Console.WriteLine(obj2==obj1);//引用类型比较引用 输出False
2. Equals
1. Used to compare whether the references of two objects are equal.
2. However, for value types, if the type is the same (no automatic type conversion will be performed), and the value is the same (each member of the struct must be the same), then Equals returns
true, otherwise return false.
3. For reference types, the default behavior is the same as that of ReferenceEquals. True is returned only when two objects point to the same Reference.
4. Equals can be overloaded as needed
5. Instance
int i = 5; int j = 5; Console.WriteLine(i.Equals(j));//值类型比较 输出True int m = 6; double n = 6.0; Console.WriteLine(m.Equals(n));//类型不会自动转换并比较数值 输出False object obj1 = new object(); object obj2 = new object(); Console.WriteLine(obj2.Equals(obj1));//引用类型比较 输出False Console.WriteLine(obj2.Equals(string.Empty));//输出False,比较量对象的类型不同直接返回False
3. ReferenceEquals
1. Static method of Object, compares whether the references of two objects are equal, value type and reference type It's all the same.
2. This method cannot be overridden in inherited classes. The prototype is: public static bool ReferenceEquals(object objA, object
objB);FCL has been implemented for us. It is to compare whether the memory address pointed to by the reference is the same.
3. For two value types, ReferenceEquals is always false because ReferenceEquals(object a,object
b) After the method, the value type is reboxed into a new reference type instance, and naturally there is no reference equality.
4. For two reference types, ReferenceEquals will compare whether they point to the same address.
5. Examples
int i = 5; int j = 5; Console.WriteLine(object.ReferenceEquals(i, j));//输出False int m = 6; double n = 6.0; Console.WriteLine(object.ReferenceEquals(m, n));//输出False object obj1 = new object(); object obj2 = new object(); Console.WriteLine(object.ReferenceEquals(obj1, obj2));//输出False
The above are the differences between ==, Equals, and ReferenceEquals. I hope it will be helpful to everyone's learning. For more related articles, please pay attention to the PHP Chinese website (www.php.cn) !