Why returning false ? new Person("james") == new Person("james")?
Asked Answered
R

5

8

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;
    }
}
Resolved answered 3/8, 2010 at 11:9 Comment(0)
E
26

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.

Ebony answered 3/8, 2010 at 11:10 Comment(2)
Can u explain me how reference equality can determine that both are same address on memory or not ?Resolved
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".Berk
D
5

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

Dittmer answered 3/8, 2010 at 11:11 Comment(0)
O
2

== 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

Osmanli answered 3/8, 2010 at 11:12 Comment(0)
B
2

Yes dtb is right you want

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

instead

Bedmate answered 3/8, 2010 at 11:12 Comment(1)
Already as expected Equals method provide true always so it will return true but wonder why == operator does notResolved
F
2

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.

Fletafletch answered 3/8, 2010 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.