Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Roslyn
  3. Generator.Equals

Generator.Equals

MITC#4.0.1

A C# source generator that automatically implements IEquatable<T>, Equals, and GetHashCode using attributes.

GitHubGitHub
193 stars22 forks0 contributors

What is Generator.Equals?

Generator.Equals is a C# source generator that automatically implements the `IEquatable<T>` interface, along with `Equals` and `GetHashCode` methods, using attributes. It solves the problem of writing and maintaining correct, efficient equality logic, which is tedious and error-prone when done manually. The generator produces the code at compile time, ensuring best practices and eliminating boilerplate.

Target Audience

C# developers working on projects where value equality is important, such as those using domain models, data transfer objects, or any type that needs correct `Equals` and `GetHashCode` implementations.

Value Proposition

Developers choose Generator.Equals because it drastically reduces manual coding and potential bugs in equality logic, supports complex scenarios like collections and inheritance out of the box, and provides detailed diff reporting, all through a simple, declarative attribute-based API.

Overview

A source code generator for automatically implementing IEquatable<T> using only attributes.

Use Cases

Best For

  • Automatically generating equality implementations for C# records, classes, and structs
  • Comparing collections (arrays, lists, dictionaries) by value instead of reference
  • Implementing complex equality logic with custom comparers or precision tolerances
  • Handling inheritance hierarchies where equality needs to consider base class properties
  • Getting detailed reports on differences between two object instances for debugging or auditing
  • Eliminating boilerplate code for IEquatable<T> in large codebases

Not Ideal For

  • Projects stuck on .NET frameworks or C# versions below 9.0, as it requires LangVersion >= 9.0
  • Scenarios requiring runtime equality logic generation for dynamic or reflection-based types, since it's compile-time only
  • Teams with strict 'no external source generators' policies due to build process or tooling constraints
  • Simple data transfer objects where C# records' built-in equality suffices without the added attribute complexity

Pros & Cons

Pros

Zero Boilerplate Implementation

Marking a type with [Equatable] automatically generates IEquatable<T>, Equals, and GetHashCode, eliminating tedious and error-prone manual coding, as shown in the record example.

Collection-Aware Value Comparisons

Supports arrays, lists, dictionaries, and sets with attributes like [OrderedEquality] and [SetEquality], enabling deep value-based comparisons out of the box.

Highly Customizable Per-Property

Offers attributes for string comparison, numeric precision, references, and custom comparers, allowing fine-grained control without rewriting entire equality logic.

Inheritance and Deep Equality

Correctly chains base.Equals() across hierarchies and inherits attributes from overridden properties, ensuring deep equality without manual boilerplate, as detailed in the migration notes.

Structured Diff Reporting

Provides an Inequalities() method that returns detailed member-level differences with paths into nested objects and collections, useful for debugging and auditing.

Cons

C# Version Dependency

Requires setting LangVersion to 9.0 or higher, which can block adoption in legacy projects or environments with fixed toolchains.

Attribute Complexity Overhead

Developers must learn and correctly apply multiple attributes (e.g., [CustomEquality], [PrecisionEquality]), which can be overkill for simple types compared to hand-written code.

No Runtime Flexibility

As a compile-time source generator, it cannot adapt to runtime type changes or dynamic objects, limiting use in highly reflective or plugin-based architectures.

Potential Performance Trade-offs

Features like collection comparisons and structured diffs add overhead; for instance, [SetEquality] has hashing limitations and can be expensive for large collections.

Frequently Asked Questions

Quick Stats

Stars193
Forks22
Contributors0
Open Issues4
Last commit25 days ago
CreatedSince 2020

Tags

#boilerplate-reduction#equality-comparison#csharp#source-generator#dotnet#csharp-sourcegenerator#roslyn-analyzer#source-generators#code-generation#compile-time

Built With

.
.NET
C
C++

Included in

Roslyn734
Auto-fetched 18 hours ago

Related Projects

StrongInjectStrongInject

compile time dependency injection for .NET

Stars868
Forks25
Last commit1 year ago
JsonSrcGenJsonSrcGen

Json library that uses .NET 5 Source Generators

Stars151
Forks5
Last commit5 years ago
StructPackerStructPacker

Low-level, lightweight and performance-focused serializer for C# struct types that uses Source Generators technology.

Stars81
Forks7
Last commit4 years ago
WrapperValueObjectWrapperValueObject

A .NET source generator for creating simple value objects wrapping primitive types.

Stars52
Forks3
Last commit3 years ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub