是否有一个完整的IEquatable实现参考

是否有一个完整的IEquatable实现参考

本文介绍了是否有一个完整的IEquatable实现参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的很多问题在这里对SO关注IEquatable实施。我发现它是非常困难的正确实施,因为有很多隐藏的错误在幼稚的,我发现关于它的文章是相当不完整的。我想找到或编写一个明确的参考,其中必须包括:

Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include:

  • 如何正确实施IEquatable
  • 如何重写正确等于
  • 如何覆盖GetHash code正确
  • 如何正确实现ToString方法
  • 如何正确地执行操作==
  • 如何!=正确执行操作

这样一个完整的参考已经存在?

Such a complete reference already exists?

PS:偶参考MSDN 似乎有缺陷我

PS: Even MSDN reference seems flawed to me

推荐答案

我终于写了一个样本code,显示实现所有IEquatable相关的方法正确的方法:

I finally wrote a sample code that shows the correct way to implement all IEquatable related methods:

<一个href="http://$c$c.google.com/p/iequatable-implementation-reference/source/browse/IEquatableReference/trunk/IEquatableReference/ValuesClass.cs">http://$c$c.google.com/p/iequatable-implementation-reference/source/browse/IEquatableReference/trunk/IEquatableReference/ValuesClass.cs

这证明了code每行的单元测试有:

The unit tests that justify each line of code are there:

<一个href="http://$c$c.google.com/p/iequatable-implementation-reference/source/browse/IEquatableReference/trunk/IEquatableReferenceTest/ValuesClassTest.cs">http://$c$c.google.com/p/iequatable-implementation-reference/source/browse/IEquatableReference/trunk/IEquatableReferenceTest/ValuesClassTest.cs

我只是不写单元测试GetHash code算术溢出。

I just didn't write a unit test for GetHashCode arithmetic overflow.

这篇关于是否有一个完整的IEquatable实现参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:34