Employee emp1 = new Employee {Name = "Swapnil",Age = 27 };
Employee emp2 = new Employee { Name = "Swapnil", Age = 27 };
if (object.Equals(emp1, emp2))
{
}
else
{
}
This code is not able to compare.
How i can compare two Object s in C# ?
Without overriding the Equals method only a reference comparison willoccur. You want to perform a value comparison.
Override the method in your Employee class like so:
Then compare the objects like so:
OR with the comparison operator:
More details on MSDN here: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx