views:

363

answers:

5

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;
    }
}
+24  A: 

Because the == operator defaults to reference equality. It doesn't call your Equals method.

You can override the == operator if you want. See: Guidelines for Overriding Equals() and Operator ==

In C#, there are two different kinds of equality: reference equality (also known as identity) and value equality. Value equality is the generally understood meaning of equality: it means that two objects contain the same values. For example, two integers with the value of 2 have value equality. Reference equality means that there are not two objects to compare. Instead, there are two object references and both of them refer to the same object.

[...]

By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.

dtb
Can u explain me how reference equality can determine that both are same address on memory or not ?
Freshblood
Step 1: Framework reads memory address for object one. Step 2: Framework reads memory address for object two. Step 3: Are the numbers equal? Step 4: It's spelled "you".
Joel Mueller
+4  A: 

You have to separately override the == operator if that's really what you want.

http://msdn.microsoft.com/en-us/library/ms173147%28VS.80%29.aspx

Nick
+2  A: 

== is a reference equality operator in this case. It compares if two references are identical.

The new operator always create a new object, so a new Something() will NEVER be an identical reference to another new Something().

You can override the == operator to perform value comparison instead of reference comparison. This is what e.g. String does.

See also

Related questions

polygenelubricants
+1  A: 

Yes dtb is right you want

bool b = new Person("james").Equals(new Person("james")); 

instead

w69rdy
Already as expected Equals method provide true always so it will return true but wonder why == operator does not
Freshblood
+1  A: 

The == operator checks whether two variables are in fact, literally references to the same object in memory. In your example you create two James. They might be twins (i.e. they might have the identical memory-footprint), but they're not the same person (i.e. they have two different memory locations). If you wrote:

Person a = new Person("james");
Person b = a;
Console.WriteLine(a == b); 

you would get true, because a and b are just two names for the same James.

apropoz