Menu

operators-equals

Will Pittenger

As an assignment operator

In order for the = operator to work as the assignment operator, the only thing that can be to the left of it is the variable you're assigning a value to. This can be its declaration or an existing instance. The left operand must be an l-value.

As a comparison operator

This compares the two operands based on the rules below:

  1. If the operands are reference types, it returns true if the references are for the same object and false otherwise.
  2. If the operands both implement IEquatable, it returns the return value from "leftOperand.equals rightOperand".
  3. Otherwise, for primitive types only, it returns true if the bits stored in both instances match and false if they don't.

If the type is a structure that doesn't implement IEquatable, it should fail to compile. If you need to compare instances of a reference type, consider using IEquatable or IComparable. If one operand is a reference type and the other is a by value type, the compiler will attempt to use IEquatable on the value type. If that fails, it should fail to compile.


Related

Wiki: Operators
Wiki: operators-inequality