2

If reference type doesn't overload an equality operator ==, then build-in equality operator on Object will be used instead. Why isn't the same true for user-defined structs:

struct A{ }

static void Main(string[] args)
{
    A a = new A();
    A a1 = new A();
    bool equal= (a == a1); //error
} 

Namely, doesn't ValueType ( from which all structs derive ) also overload == operator?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user702769
  • 2,435
  • 2
  • 25
  • 34

2 Answers2

5

How would such a default == operator work? For reference types, comparing adresses is reasonable, but since that check will never be true for two ValueTypes (since if two ValueTypes are in scope then they are guaranteed to have different locations on the stack,) address comparison is pointless.

As the compiler has helpfully pointed out, ValueType very intentionally does not have a default == operator.

dlev
  • 48,024
  • 5
  • 125
  • 132
  • 1
    @dlev, then how would you explain the fact that you can write `bool equal = (5 == 6);`. After all Int32 is a value type as well. – Darin Dimitrov May 12 '11 at 19:37
  • @Darin, would you consider that == the "default", insofar as the two Int32's are being compared without consideration for their Int32-ness? – dlev May 12 '11 at 19:43
  • @Darin: that's because _some_ value types, like Int32, override `==`. It still isn't default. – H H May 12 '11 at 19:44
  • 2
    "How would such a default == operator work?" - by calling the default Equals member. – H H May 12 '11 at 19:50
  • 1
    @Darin the reason that you can write `bool equal = (5 == 6);` is that the C# compiler translates the `==` operator into a `ceq` IL instruction. `System.Int32` doesn't define any operators. – Wesley Wiser May 12 '11 at 19:54
4

Structs probably don't provide a default == operator implementation because, unlike a class instance, a struct has no concept of reference-style identity.

From the guidelines:

Implementing the Equality Operator (==) on Value Types

In most programming languages there is no default implementation of the equality operator (==) for value types. Therefore, you should overload == any time equality is meaningful.

You should consider implementing the Equals method on value types because the default implementation on System.ValueType will not perform as well as your custom implementation.

Implement == any time you override the Equals method.

However, structs do provide a default Equals Method implementation which will do a memberwise compare using reflection.

Mark Simpson
  • 23,245
  • 2
  • 44
  • 44
  • No, there is no default for structs. There is an Equals and an == for anonymous types. – H H May 12 '11 at 19:40
  • You've mis-read what I posted. There definitely is a default _Equals method_ implementation for structs, but not an == operator implementation. – Mark Simpson May 12 '11 at 19:43
  • I've edited it to place emphasis on the method in case it's not clear enough, cheers. – Mark Simpson May 12 '11 at 19:44