I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Person("james") == new Person("james"));
Console.ReadKey();
}
}
class Person
{
private string Name;
public Person(string name)
{
Name = name;
}
public override int GetHashCode()
{
return 1;
}
public override bool Equals(object obj)
{
return true;
}
}
Because the
==operator defaults to reference equality. It doesn’t call yourEqualsmethod.You can override the
==operator if you want. See: Guidelines for Overriding Equals() and Operator ==