iequatable

What's the difference between IEquatable and just overriding Object.Equals()?

喜你入骨 提交于 2019-11-26 18:16:10
I want my Food class to be able to test whenever it is equal to another instance of Food . I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable<Food> or just override Object.Equals() ? From MSDN: This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable.Equals method for T (the type of values in the list). So my next question is: which functions/classes of the .NET framework make use of Object.Equals() ? Should I use it in the first place? The main reason is

Is there a complete IEquatable implementation reference?

女生的网名这么多〃 提交于 2019-11-26 17:57:50
问题 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: How to implement IEquatable correctly How to override Equals correctly How to override GetHashCode correctly How to implement the ToString method correctly How to implement the

What is the difference between IEqualityComparer<T> and IEquatable<T>?

一个人想着一个人 提交于 2019-11-26 15:15:54
问题 I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. 回答1: IEqualityComparer<T> is an interface for an object that performs the comparison on two objects of the type T . IEquatable<T> is for an object of type T so that it can compare itself to another. 回答2: When deciding whether to use IEquatable<T> or IEqualityComparer<T>, one could ask: Is there a preferred way of testing two instances of T for

What&#39;s the difference between IEquatable and just overriding Object.Equals()?

人走茶凉 提交于 2019-11-26 06:13:40
问题 I want my Food class to be able to test whenever it is equal to another instance of Food . I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable<Food> or just override Object.Equals() ? From MSDN: This method determines equality by using the default equality comparer, as defined by the object\'s implementation of the IEquatable.Equals method for T (the type of values in the list). So my next question is: which functions/classes of the

Distinct not working with LINQ to Objects

我们两清 提交于 2019-11-26 03:15:51
问题 class Program { static void Main(string[] args) { List<Book> books = new List<Book> { new Book { Name=\"C# in Depth\", Authors = new List<Author> { new Author { FirstName = \"Jon\", LastName=\"Skeet\" }, new Author { FirstName = \"Jon\", LastName=\"Skeet\" }, } }, new Book { Name=\"LINQ in Action\", Authors = new List<Author> { new Author { FirstName = \"Fabrice\", LastName=\"Marguerie\" }, new Author { FirstName = \"Steve\", LastName=\"Eichert\" }, new Author { FirstName = \"Jim\", LastName=