> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Equality

The `==` (equality) operator is a binary comparison operator that evaluates whether its two operands are equal, returning a `bool` value. Its underlying evaluation mechanism depends strictly on the type of the operands being compared: built-in primitives evaluate for numeric or boolean equality, while reference types default to evaluating for reference equality (identity), unless the operator is explicitly overloaded.

```csharp theme={"dark"}
bool result = operand1 == operand2;
```

## Evaluation Mechanics by Type

**Value Types (`struct`, `enum`, primitives)**
For built-in primitive value types (e.g., `int`, `bool`, `char`), the operator evaluates numeric or boolean equality using direct Intermediate Language (IL) instructions. For `enum` types, the operator compares the underlying integral values. For user-defined `struct` types, the `==` operator is not available by default and will result in a compiler error unless explicitly overloaded.

**Reference Types (`class`, `object`)**
For standard reference types, the operator evaluates reference equality. It returns `true` only if both operands point to the exact same memory address on the managed heap. It does not inspect the data contained within the objects.

**Strings (`string`)**
Although `string` is a reference type, the .NET framework overloads the `==` operator to perform an ordinal, case-sensitive value comparison of the underlying character sequences. The implementation explicitly performs a reference equality check (`object.ReferenceEquals`) as an initial fast-path optimization before falling back to a character-by-character comparison.

**Delegates (`delegate`)**
Delegate types (derived from `System.MulticastDelegate`) overload the `==` operator to perform value-based equality on their invocation lists. Two distinct delegate instances will evaluate to `true` if they point to the exact same target instances and method pointers in the same order.

**Record Types (`record class`, `record struct`)**
For both reference-based records (`record` or `record class`) and value-based records (`record struct`, introduced in C# 10), the compiler automatically synthesizes an overloaded `==` operator. This generated operator performs value-based equality by executing a member-wise comparison of all properties and fields.

**Tuple Types (`ValueTuple`)**
Since C# 7.3, tuple types have built-in support for the `==` operator. The compiler evaluates tuple equality by performing a short-circuiting, element-wise comparison of the corresponding tuple elements in their declared order.

**Nullable Value Types (`Nullable<T>`)**
For nullable value types, the compiler provides "lifted" operators. If both operands are `null`, `==` evaluates to `true`. If one operand is `null` and the other has a value, it evaluates to `false`. If both operands possess values, the operator unwraps them and applies the underlying type `T`'s `==` operator.

**Floating-Point Types (`float`, `double`)**
Floating-point equality adheres to IEEE 754 standards. Consequently, comparing `NaN` (Not a Number) yields `false` even when compared against itself (`Double.NaN == Double.NaN` evaluates to `false`).

## Generics

Comparing two instances of an unconstrained generic type parameter (`t1 == t2`) results in a compiler error (CS0019). The compiler cannot guarantee that the unknown type substituted for `T` will support the `==` operator. However, comparing an unconstrained generic operand to `null` (e.g., `t1 == null`) is perfectly valid C# and compiles successfully. The compiler handles the `null` check dynamically based on whether `T` resolves to a reference type or a value type at runtime.

To use `==` to compare two generic instances to each other, the type parameter must be constrained:

* **Reference Type Constraint:** Constraining `T` to `class` (`where T : class`) instructs the compiler to emit a reference equality check.
* **Base Class Constraint:** Constraining `T` to a specific base class that overloads the `==` operator instructs the compiler to emit a call to that specific overloaded operator.
* **Static Abstract Interface Constraint (C# 11+):** `T` can be constrained to an interface with static abstract operators, such as `where T : IEqualityOperators<T, T, bool>`. This allows the `==` operator to be resolved generically for any conforming type, including value types.

## Operator Overloading

The `==` operator can be redefined for user-defined types using the `operator` keyword. C# enforces a strict pairing rule: if a type overloads the `==` operator, it must also overload the `!=` (inequality) operator.

When overloading `==`, developers must also override the virtual `Object.Equals(object)` method and `Object.GetHashCode()` to prevent compiler warnings (CS0660 and CS0661) and ensure logical consistency across the .NET ecosystem (e.g., when the type is used as a key in a hash-based collection).

```csharp theme={"dark"}
public struct Vector2
{
    public int X { get; }
    public int Y { get; }

    public Vector2(int x, int y)
    {
        X = x;
        Y = y;
    }

    public static bool operator ==(Vector2 left, Vector2 right)
    {
        return left.X == right.X && left.Y == right.Y;
    }

    public static bool operator !=(Vector2 left, Vector2 right)
    {
        return !(left == right);
    }

    public override bool Equals(object? obj)
    {
        return obj is Vector2 other && this == other;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(X, Y);
    }
}
```

## Intermediate Language (IL) Representation

At the compiler level, the behavior of `==` dictates the emitted Common Intermediate Language (CIL):

* For built-in primitives and reference identity checks, the compiler emits the `ceq` (compare equal) opcode.
* For types with an overloaded `==` operator, the compiler emits a `call` instruction invoking the static method `op_Equality(Type, Type)` generated by the compiler for that specific type.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
