Advertisement

01.23.2008 at 11:24AM PST, ID: 23105524
[x]
Attachment Details

Overloading the == operator and the GetHashCode() method...

Asked by kalliopi in .Net Editors & IDEs, Microsoft Visual C#.Net, .NET Framework 2.0

Tags: int, method, unique

Hi,

I have a class called foo (below) that overloads the == operator.  But I'm getting a warning on compile:

'foo' defines operator == or operator != but does not override Object.GetHashCode()

This makes sense since I could have two totally seperate instances in memory, but if they have the same Id, they would return true for ==, but would generate two different hash codes since the base implementation is based on their location in memory.  The question is how can I reliably convert my unique Guid Id field into a reliable int hash code to use in my overriden GetHashCode() method?
Start Free Trial
1:
2:
3:
4:
5:
6:
public class foo {
  public Guid Id;
  public static bool operator ==(foo f1, foo f2) {
    return f1.Id == f2.Id;
  }
}
 
 
[+][-]01.23.2008 at 11:29AM PST, ID: 20726586

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.23.2008 at 12:06PM PST, ID: 20726999

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.23.2008 at 12:09PM PST, ID: 20727035

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.23.2008 at 12:30PM PST, ID: 20727325

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.23.2008 at 02:16PM PST, ID: 20728495

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.23.2008 at 02:30PM PST, ID: 20728611

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.23.2008 at 02:32PM PST, ID: 20728627

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.23.2008 at 03:41PM PST, ID: 20729156

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.23.2008 at 05:08PM PST, ID: 20729735

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, Microsoft Visual C#.Net, .NET Framework 2.0
Tags: int, method, unique
Sign Up Now!
Solution Provided By: GreenGhost
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.24.2008 at 07:46AM PST, ID: 20734020

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906