U2U.ValueObjectComparers 2.2.0

Quickly and efficiently implement the Equals and GetHashCode for value objects

Install-Package U2U.ValueObjectComparers -Version 2.2.0
dotnet add package U2U.ValueObjectComparers --version 2.2.0
<PackageReference Include="U2U.ValueObjectComparers" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add U2U.ValueObjectComparers --version 2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Here is an example of what a Value Object looks like. No inheritance, so this can also be used for structs.

The Equals method simply delegates to the ValueObjectComparer.
Same for the IEquatable interface implementation. The IEquatable interface
uses a strongly typed Equals method, which is better for structs, as it will avoid boxing.

The difference between the built-in object's Equals and IEquatable is that the compiler will pick the most specific matching Equals(T other).

public class SomeObject : IEquatable<SomeObject>
{
  public string Name { get; set; }
  public int Age { get; set; }

  public override bool Equals(object obj)
    => ValueObjectComparer<SomeObject>.Instance.Equals(this, obj);

  public bool Equals([AllowNull] SomeObject other) 
    => ValueObjectComparer<SomeObject>.Instance.Equals(this, other);
}

Ignoring Certain Properties

Some Value Objects have calculated properties, and using them in the comparison is not necessary. You might want to use the ValueObjectComparer for other, non-value-object, types. So you might want to ignore certain properties for short.

Simply add the [Ignore] attribute to the property, and it won't be used for equality.

[Ignore]
public int NotUsed { get; set; }

You can find more information here, here, and here.

Here is an example of what a Value Object looks like. No inheritance, so this can also be used for structs.

The Equals method simply delegates to the ValueObjectComparer.
Same for the IEquatable interface implementation. The IEquatable interface
uses a strongly typed Equals method, which is better for structs, as it will avoid boxing.

The difference between the built-in object's Equals and IEquatable is that the compiler will pick the most specific matching Equals(T other).

public class SomeObject : IEquatable<SomeObject>
{
  public string Name { get; set; }
  public int Age { get; set; }

  public override bool Equals(object obj)
    => ValueObjectComparer<SomeObject>.Instance.Equals(this, obj);

  public bool Equals([AllowNull] SomeObject other) 
    => ValueObjectComparer<SomeObject>.Instance.Equals(this, other);
}

Ignoring Certain Properties

Some Value Objects have calculated properties, and using them in the comparison is not necessary. You might want to use the ValueObjectComparer for other, non-value-object, types. So you might want to ignore certain properties for short.

Simply add the [Ignore] attribute to the property, and it won't be used for equality.

[Ignore]
public int NotUsed { get; set; }

You can find more information here, here, and here.

  • .NETStandard 2.1

    • 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.

Version History

Version Downloads Last updated
2.2.0 101 4/7/2020
0.2.0 268 12/31/2019
0.1.1 158 12/30/2019
0.1.0 111 12/26/2019