NCompare 2.0.0
dotnet add package NCompare --version 2.0.0
NuGet\Install-Package NCompare -Version 2.0.0
<PackageReference Include="NCompare" Version="2.0.0" />
<PackageVersion Include="NCompare" Version="2.0.0" />
<PackageReference Include="NCompare" />
paket add NCompare --version 2.0.0
#r "nuget: NCompare, 2.0.0"
#:package NCompare@2.0.0
#addin nuget:?package=NCompare&version=2.0.0
#tool nuget:?package=NCompare&version=2.0.0
NCompare
The library provides the ComparerBuilder<T> type, which helps to create comparators (equality and sort comparators) for comparing objects of type T.
ComparerBuilder<T> helps to compare complex types. It can create EqualityComparer<> and/or a Comparer<> objects for your types.
You just need to "add" expressions, that describe what data you want compare and, optionally, comparators - to specify how to compare that data.
The main goal of having this type is to set once and uniformly according to what rules you need to compare objects, and after that be able
to get both IEqualityComparer<T> there and IComparer<T>.
You can find reasons for making ComparerBuilder<> in this old post on RSDN (in Russian).
In the future, I plan to expand the library with comparators for various collections. This will allow you to quickly and conveniently create comparators for complex and interconnected objects.
Now ComparerBuilder<T> is written on C# 14.0. it builds for .NET Framework 4.6.2 and .NET Standard 2.0 so could
be used in many projects.
Examples
Let's imagine that we need to add comparison logic to the Person type below:
public sealed class Person
{
public string Name { get; set; } = String.Empty;
public DateTime? BirthDate { get; set; }
public override string ToString() => $"\"{Name}\", #{BirthDate:yyyy-MM-dd}#";
}
This can be done like this:
internal static class PersonComparison
{
internal static ComparerBuilder<Person> PersonComparerBuilder { get; } = new ComparerBuilder<Person>() // Creating an instance of the builder
// Add comparison for `Name` property. Using `OrdinalIgnoreCase` comparer for both, equality and sorting.
// Custom `IEqualityComparer<>` and `IComparer<>` also could be provided, one of them or both.
.Add(item => item.Name, StringComparer.OrdinalIgnoreCase)
// Compare only `Date` part with default comparators (for equality and sorting)
.Add(item => item.BirthDate != null ? item.BirthDate.Value.Date : default(DateTime?));
// Actually, build comparators
public static EqualityComparer<Person> PersonEqualityComparer { get; } = PersonComparerBuilder.CreateEqualityComparer();
public static Comparer<Person> PersonComparer { get; } = PersonComparerBuilder.CreateComparer();
}
Results:
[TestMethod]
public void PersonComparisonTests() {
var person1 = new Person { Name = "Alex", BirthDate = new DateTime(2000, 01, 01, 07, 00, 00), };
var person2 = new Person { Name = "aleX", BirthDate = new DateTime(2000, 01, 01, 16, 30, 00), };
var person3 = new Person { Name = "Bob", };
Assert.AreEqual(person1, person2, PersonComparison.PersonEqualityComparer); // The first two are equal
Assert.AreNotEqual(person2, person3, PersonComparison.PersonEqualityComparer); // The third is different
var compare12 = PersonComparison.PersonComparer.Compare(person1, person2);
Assert.AreEqual(expected: 0, compare12);
var compare23 = PersonComparison.PersonComparer.Compare(person2, person3);
Assert.IsTrue(compare23 < 0);
}
For more complicated examples, including self-references and mutually referential types please visit project's web site https://github.com/ViIvanov/NCompare.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- System.ValueTuple (>= 4.6.1)
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
* Minimum supported .NET Framework version changed to 4.6.2 (because 4.6.2 is out of support)
* Project upgraded to .NET 10 and C# 14
* Referenced packages updated to the latest versions
* Improvements in the generation of `CompareTo(…)` for the case when the custom comparer is not provided