Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .Equals and == [duplicate]

Tags:

string

c#

.net

What is the difference between a.Equals(b) and a == b for value types, reference types, and strings? It would seem as though a == b works just fine for strings, but I'm trying to be sure to use good coding practices.

like image 411
CodeMonkey1313 Avatar asked Apr 21 '09 14:04

CodeMonkey1313


People also ask

Is == and .Equals the same?

In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects. If a class does not override the equals method, then by default, it uses the equals(Object o) method of the closest parent class that has overridden this method.

What is the difference between == and .Equals method when to use which?

== checks if both references points to same location or not. equals() method should be used for content comparison. equals() method evaluates the content to check the equality.

What is the difference between == and .Equals method in C#?

CSharp Online TrainingThe 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.

What does == === mean?

The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.


2 Answers

From When should I use Equals and when should I use ==:

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.

using System;  public class Test {     static void Main()     {         // Create two equal but distinct strings         string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});         string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});          Console.WriteLine (a==b);         Console.WriteLine (a.Equals(b));          // Now let's see what happens with the same tests but         // with variables of type object         object c = a;         object d = b;          Console.WriteLine (c==d);         Console.WriteLine (c.Equals(d));     } } 

The result of this short sample program is

True True False True 
like image 58
Dirk Vollmar Avatar answered Sep 19 '22 22:09

Dirk Vollmar


Here is a great blog post about WHY the implementations are different.

Essentially == is going to be bound at compile time using the types of the variables and .Equals is going to be dynamically bound at runtime.

like image 34
Chris Shaffer Avatar answered Sep 22 '22 22:09

Chris Shaffer