dotnet / dotnet/dotnet-api-docs
.Net 8+ LINQ examples with EqualityComparer.Create
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
in .Net 8 new class was created to allow using lambdas anywhere that IQualityComparer is used. According to [https://github.com/dotnet/runtime/issues/101448](https://github.com/dotnet/runtime/issues/101448) the reasoning was that adding lambda functionality everywhere IEqualityComparer is used would become a maintenance headache.
Unfortunately, [EqualityComparer.Create](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.equalitycomparer-1.create) is very new and not well enough known to show up on google searches or otherwise be easily discoverable.
I propose adding small example snippets that demonstrate this new functionality so that it can be more easily discovered. For example: in [Enumerable.SequenceEqual(IEnumerable, IEnumerable, IEqualityComparer)](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-9.0#system-linq-enumerable-sequenceequal-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0))))
The following example demonstrates how a lambda function can be used to compare to sequences:
```csharp
ProductA[] storeA = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 } };
ProductA[] storeB = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 } };
IEqualityComparer comparer = EqualityComparer.Create((first,second) => first.Code == second.Code);
bool equalAB = storeA.SequenceEqual(storeB, comparer);
Console.WriteLine("Equal? " + equalAB);
/*
This code produces the following output:
Equal? True
*/
```
Contributor guide
Assessment
This issue has not been assessed yet.