Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

5 ways for equality check in .net .. why? and which to use?

Tags:

c#

.net

While learning .net (by c#) i found 5 ways for checking equality between objects.

  1. The ReferenceEquals() method.
  2. The virtual Equals() method. (System.Object)
  3. The static Equals() method.
  4. The Equals method from IEquatable interface.
  5. The comparison operator == .

My question is :

  1. Why there are so many Equals() method along with comparison operator?
  2. Which one of the virtual Equals() or IEquatable's Equals() sholud be used .. (say if we use our own collection classes)
like image 678
Gaurav Avatar asked Jul 07 '10 04:07

Gaurav


People also ask

What is the difference between the equality operator == and equals () method in C#?

Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.

How do you check if two values are equal in C#?

You can use the == operator, as shown in the following example. int a = GetOriginalValue(); int b = GetCurrentValue(); // Test for value equality. if (b == a) { // The two integers are equal. }

Which of the following is the equality operator C#?

The most common way to compare objects in C# is to use the == operator. For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise.

How do you test for equality in Java?

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


1 Answers

1 - Reference equals checks if two reference type variables(classes, not structs) are referred to the same memory adress.

2 - The virtual Equals() method checks if two objects are equivalent. Let us say that you have this class:

class TestClass{     public int Property1{get;set}     public int Property2{get;set}      public override bool Equals(object obj)     {         if (obj.GetType() != typeof(TestClass))             return false;          var convertedObj = (TestClass)obj;          return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);     } } 

and you instantiate 2 objects from that class:

var o1 = new TestClass{property1 = 1, property2 = 2} var o2 = new TestClass{property1 = 1, property2 = 2} 

although the two objects are not the same instance of TestClass, the call to o1.Equals(o2) will return true.

3 - The static Equals method is used to handle problems when there is a null value in the check. Imagine this, for instance:

TestClass o1 = null; var o2 = new TestClass{property1 = 1, property2 = 2} 

If you try this:

o1.Equals(o2); 

you wil get a NullReferenceException, because o1 points to nothing. To adress this issue, you do this:

Object.Equals(o1,o2);

This method is prepared to handle null references.

4 - The IEquatable interface is provided by .Net so you don't need to do casts inside your Equals method. If the compiler finds out that you have implemented the interface in a class for the type you are trying to check for equality, it will give that method priority over the Object.Equals(Object) override. For instance:

class TestClass : IEquatable<TestClass> {     public int Property1 { get; set; }     public int Property2 { get; set; }      public override bool Equals(object obj)     {         if (obj.GetType() != typeof(TestClass))             return false;          var convertedObj = (TestClass)obj;          return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);     }      #region IEquatable<TestClass> Members      public bool Equals(TestClass other)     {         return (other.Property1 == this.Property1 && other.Property2 == this.Property2);     }      #endregion } 

now if we do this:

var o1 = new TestClass{property1 = 1, property2 = 2} var o2 = new TestClass{property1 = 1, property2 = 2} o1.Equals(o2); 

The called method is Equals(TestClass), prior to Equals(Object).

5 - The == operator usually means the same as ReferenceEquals, it checks if two variables point to the same memory adress. The gotcha is that this operator can be overrided to perform other types of checks. In strings, for instance, it checks if two different instances are equivalent.

This is a usefull link to understand equalities in .Net better:

  • CodeProject
like image 53
mverardo Avatar answered Oct 19 '22 08:10

mverardo