Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common misunderstandings of Equals method by beginners of C #

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the common misunderstandings of Equals methods among beginners of C#?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

As we all know, there are two kinds of equality in the world of C #. One is logical equivalence: if two objects logically represent the same value, they are said to be logically equivalent. The other is reference equality: if two references point to the same object instance, they are said to have reference equality.

It is well known that the Object type has an instance method called Equals that can be used to determine whether two objects are equal. The default implementation of Object's Equals compares the reference equality of two objects. ValueTpye, a derived class of Object, overrides the Equals method, which compares the logical equivalence of two objects.

That is, in C #, the default Equals version of reference types focuses on reference equality, while value types focus on logical equality. Of course, this does not always meet our requirements. So whenever we care more about the logical equivalence of reference types, we should override the Equals method.

A famous example of overriding the Equals method of a reference type to change its default comparison is the String class. When we write code like "string1.Equals (string2)", we compare not whether the two references string1 and string2 point to the same instance (reference equivalence), but whether string1 and string2 contain the same sequence of characters (logical equivalence).

Misunderstanding 1: the Equals method and operator== have the same default behavior.

For a reference type, if the = = operator is not overridden for it, and its parent type does not override the Equals method, the reference type Equals method and operator== have the same default behavior, that is, they both compare the reference equality of the object. For value types, however, this is not the case at all! Because if you don't overload operator== for a custom value type, you can't write the code "myStruct1 = = myStruct2", or you'll get a compilation error because the value type doesn't have a default implementation of equality operator overloading.

Misunderstanding 2: the default implementation of the Equals method of the custom class will automatically call the operator== method, or the default implementation of the operator== method will automatically call the Equals method.

It is often heard that so-and-so is a reference type, so the default implementation of its Equals method will automatically call the operator== method. This argument is totally unreasonable. As mentioned above, the default implementation of the reference type Equals method comes from Object, while the default implementation of value types comes from TypeValue, even if they use the = = operator, using an overloaded version of Object or TypeValue.

In principle, as long as we do not override the Equals method of a class, it inherits the implementation of its parent class, which does not have the opportunity to overload with the operator of the subtype. Similarly, as long as we do not call the Equals method in the = = operator overload of a class, it will not be called automatically.

Myth 3: the default Equals implementation of value types compares two objects bit by bit.

Some people think that the default implementation of Equals for value types is by comparing the bit representations of two objects in memory, that is, if all binary bits are equal, the two objects are "equal." This is not accurate. Because the default implementation of Equals for its real-value type is to call the Equals method of that field type for each field of the value type, they can only be equal if the Equals methods of all fields return true. Let's look at an example:

Class MyClass {public override bool Equals (object obj) {Console.WriteLine ("the Equals method of MyClass was called.") ; return true;} struct MyStruct {public MyClass Filed;} class Program {static void Main (string [] args) {MyStruct a; MyStruct b; a.Filed = new MyClass (); b.Filed = new MyClass (); Console.WriteLine (a.Equals (b));}}

Obviously, an and b have completely different binary representations. But the final printed result is:

The Equals method of MyClass is called. True

This means that the default implementation of a value type is determined by calling the field's Equals method to determine whether two objects are equal, rather than by comparing whether their binary bits are consistent.

Myth 4: Equals is a very basic and commonly used method, so there is no performance problem with its default implementation.

For reference types, the default implementation of Equals is simple, just to determine whether two references are of the same type and whether they point to the same block of memory. So there is no problem with its performance. But for value types, Equals's task is not that simple. It needs to compare all the fields of the two objects, that is, to call the Equals of the field type field by field.

Since it is impossible to know which fields are contained in all of its subtypes in ValueType (where the value type Equals method is implemented by default), ValueType's Equals needs to use reflection techniques in order to call the Equals method of the subtype field. As you may have seen, reflection is not a performance-friendly technique, so Equals methods of value types are not efficient. This is why Microsoft recommends that we override the Equals method for custom value types.

This is the end of the content of "what are the common misunderstandings of Equals methods among beginners of C#". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report