While learning .net (by c#) i found 5 ways for checking equality between objects.
My question is :
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.
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. }
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.
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 - 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With