What is the difference between == and Equals?

 Posted by Ddd on 1/22/2011 | Category: C# Interview questions | Views: 3783 | Points: 40

Both are used for comparison of variables, can be overloaded and return a Boolean value.

The differences are:-

Consider two variables of different types:
1) int a = 20;
string b = "20";

//Compiler error (== cannot be applied to int and string)
Console.WriteLine(a == b);

//output: False

2)int c = 455;
long f = 455;
Console.WriteLine(c == f);

//Equals strictly checks that 2 variables should be of the same type

// == gives True if 2 variables ' data types allow for similar values(Example: int and long)

3)List<string> y = new List<string>();
List<string> z = new List<string>();
Console.WriteLine(y == z);

Both will give False.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response