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

Equals is rewritten in NET and it is necessary to rewrite the sample analysis of GetHashCode

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

It is necessary to rewrite the example analysis of Equals in NET. I believe that many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Consultation area David Basarab:

Go straight to the topic and refer to the following code:

Public class Foo

{

Public int FooId {get; set;}

Public string FooName {get; set;}

Public override bool Equals (object obj)

{

Foo fooItem = obj as Foo

If (fooItem = = null)

{

Return false

}

Return fooItem.FooId = = this.FooId

}

Public override int GetHashCode ()

{

/ / Which is preferred?

Return base.GetHashCode ()

/ / return this.FooId.GetHashCode ()

}

}

The Foo here represents the row in table. Which implementation should I use when rewriting the GetHashCode () method?

Base.GetHashCode ()

This.FooId.GetHashCode ()

Answer area Marc Gravell:

If your item is to be the key in Dictionary and HashSet, then rewriting GetHashCode () is simply a rigid requirement, because the collection will group the item according to the hashcode of the item. If the hashcode of the two item is different, then the equals will never be called. The GetHashCode () method should reflect the logic of Equals. The comparison is roughly as follows:

If Equals is determined to be equal, then GetHashCode must be equal.

If the GetHashCode is determined to be equal, then the Equals is not necessarily equal.

Going back to your scenario, it makes sense to use return FooId as the implementation of GetHashCode ().

However, as a general scenario, when there are multiple attributes in item, it is recommended to combine multiple attributes as follows:

Unchecked / / only needed if you're compiling with arithmetic checks enabled

{/ (the default compiler behaviour is * disabled*, so most folks won't need this)

Int hash = 13

Hash = (hash * 7) + field1.GetHashCode ()

Hash = (hash * 7) + field2.GetHashCode ()

...

Return hash

}

It is worth mentioning that the above hashcode implementation also solves a classic diagonal conflict problem, such as: new Foo (3mem5)! = new Foo (5mem3).

Another point is that, as a habit of .NET programs, it is recommended to rewrite the = and! = operators.

Comment area

This question is actually very classic, what is the relationship between Equals and GetHashCode?

I personally think: in the comparison of complex types, GetHashCode is always a first-class citizen and Equals is a second-class citizen. First judge whether the HashCode is consistent, and then see whether the Equals is consistent in the case of inconsistency. Finally, determine whether the object is equal or not.

It is also worth noting that the implementation of GetHashCode must be efficient and complete the theoretical O (1) complexity, otherwise you will die miserably in the HashSet,Dictioanry scenario, refer to HashSet's Contains.

After reading the above, have you mastered the method that it is necessary to rewrite the sample analysis of GetHashCode after rewriting Equals in NET? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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