471,309 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Re: Finding distinct elements!

On Jun 13, 2:38 pm, CSharper <cshar...@gmx.comwrote:
here is the code
<snip>

Your Person class doesn't override Equals or GetHashCode. Therefore
every Person is distinct.

Jon
Jun 27 '08 #1
5 899
On Jun 13, 9:18 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 2:38 pm, CSharper <cshar...@gmx.comwrote:
here is the code

<snip>

Your Person class doesn't override Equals or GetHashCode. Therefore
every Person is distinct.

Jon
Hi Jon,

Thanks and I have added following method and put a break point in the
method and I noticed the method never get called.

public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
Person p = (Person)obj;
return this.Name.Equals(p.Name);
}

Thanks,
Jun 27 '08 #2
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.comwrote:
Thanks and I have added following method and put a break point in the
method and I noticed the method never get called.
<snip>

Did you override GetHashCode as well? If not, each Person would
(probably) have a distinct hash code, and thus the Distinct() method
would quite reasonably assume they weren't equal, without ever calling
your Equals method.

Jon
Jun 27 '08 #3
On Jun 13, 11:23 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 5:10 pm, CSharper <cshar...@gmx.comwrote:
Thanks and I have added following method and put a break point in the
method and I noticed the method never get called.

<snip>

Did you override GetHashCode as well? If not, each Person would
(probably) have a distinct hash code, and thus the Distinct() method
would quite reasonably assume they weren't equal, without ever calling
your Equals method.

Jon
Hi Jon,

Thanks again. Do you think the following is the correct implementation
of GetHashCode?

public override int GetHashCode()
{
return this.Name.GetHashCode();
}
Jun 27 '08 #4
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.comwrote:
Thanks again. Do you think the following is the correct implementation
of GetHashCode?

public override int GetHashCode()
{
return this.Name.GetHashCode();
}
That's fine as long as Name isn't null.

Are you sure that you really want to only judge equality by name
though? Why not include age and gender? (Date of birth is generally
better than age, btw - it doesn't change over time.) If this is just
for the purposes of learning about Distinct() that's fine, but
otherwise you might want to implement IEqualityComparer<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityComparer, but I'd be
surprised if it couldn't.

Jon
Jun 27 '08 #5
On Jun 13, 11:58 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 13, 5:42 pm, CSharper <cshar...@gmx.comwrote:
Thanks again. Do you think the following is the correct implementation
of GetHashCode?
public override int GetHashCode()
{
return this.Name.GetHashCode();
}

That's fine as long as Name isn't null.

Are you sure that you really want to only judge equality by name
though? Why not include age and gender? (Date of birth is generally
better than age, btw - it doesn't change over time.) If this is just
for the purposes of learning about Distinct() that's fine, but
otherwise you might want to implement IEqualityComparer<Person>
instead (e.g. as a NameComparer). I haven't checked whether or not
Distinct can optionally take an IEqualityComparer, but I'd be
surprised if it couldn't.

Jon
Thank you very much, you gave me good direction for me to take
further.
As always thank you for the help.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Berry | last post: by
3 posts views Thread by KL | last post: by
3 posts views Thread by andreas.maurer1971 | last post: by
5 posts views Thread by Chris Kettenbach | last post: by
2 posts views Thread by TT (Tom Tempelaere) | last post: by
1 post views Thread by Damien | last post: by
275 posts views Thread by Astley Le Jasper | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.