Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Explicit Equality operator implementation necessary

Tags:

c#

I see the point in explicitly implementing Equals and GetHashCode for my objects.

But I wonder if it makes any sense to also explicitly implement the == and != operators like this:

public static bool operator ==(Salutation left, Salutation right)
{
    return Equals(left, right);
}

Does C# not automatically use the Equals method when == is invoked?

like image 270
Tigraine Avatar asked Oct 02 '09 13:10

Tigraine


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What kind of language is C?

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system.


1 Answers

It does indeed make sense to override the equality operator along with Equals. It is in fact highly advisable.

Microsoft has posted official Guidelines for Implementing Equals and the Equality Operator (==) on MSDN. I would definitely go by the recommended practice there. The two main points are:

  • Implement the GetHashCode method whenever you implement the Equals method. This keeps Equals and GetHashCode synchronized.
  • Override the Equals method whenever you implement the equality operator (==), and make them do the same thing. This allows infrastructure code such as Hashtable and ArrayList, which use the Equals method, to behave the same way as user code written using the equality operator.

Jon Skeet also wrote a useful MSDN blog post about the subject, summarising how Equals and the == operator work by default on reference/value types.

The most important parts are quoted below:

The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour.

For reference types where == has not been overloaded, it compares whether two references refer to the same object - which is exactly what the implementation of Equals does in System.Object.

Value types do not provide an overload for == by default. However, most of the value types provided by the framework provide their own overload. The default implementation of Equals for a value type is provided by ValueType, and uses reflection to make the comparison, which makes it significantly slower than a type-specific implementation normally would be. This implementation also calls Equals on pairs of references within the two values being compared.

like image 85
Noldorin Avatar answered Oct 31 '22 22:10

Noldorin