Interview Questions

What is the difference between a.Equals(b) and a == b?

ASP.NET and .NET WEB Questions and Answers


(Continued from previous question...)

What is the difference between a.Equals(b) and a == b?

Answer1:
a=b is used for assigning the values (rather then comparison) and a==b is for comparison.

Answer2:
a == b is used to compare the references of two objects
a.Equals(b) is used to compare two objects

Answer3:
A equals b -> copies contents of b to a
a == b -> checks if a is equal to b

Answer4:
Equals method compares both type and value of the variable, while == compares value.
int a = 0;
bool b = 0

if(a.Equals(b))

Answer5:
a.Equals(b) checks whether the Type of a is equal to b or not! Put it in another way,
Dim a As Integer = 1
Dim b As Single = 1

a.Equals(b) returns false. The Equals method returns a boolean value.
a == b is a simple assignment statement.

Answer6:
a.equals(b) will check whether the “b” has same type as “a” has and also has the same data as “a” has.
a==b will do the same thing.
if you have done this in c++ under “operator overloading” than you guys must be aware of this sytaxts. they are doing the same thing there is only sytaxtical difference.
let me explain it in different manner.
a==b : means compare “b” with “a”. always left hand side expression evaluated first so here in this case “a” (considered an object) will call the overloaded operator “=” which defines “Equals(object)” method in it’s class. thus, ultimately a.equals(b) goanna called.
so the answer is: both will perform the same task. they are different by syntaxt

Answer7:
Difference b/w a==b,a.Equals(b)
a.Equals(b):
The default implementation of Equals supports reference equality only, but derived classes can override this method to support value equality.

For reference types, equality is defined as object equality; that is, whether the references refer to the same object. For value types, equality is defined as bitwise equality
== :
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

(Continued on next question...)

Other Interview Questions