GetHashCode autogeneration rant

Hi,

I just now noticed that resharper allows you to autogenerate Equals and
GetHashCode methods. Thanks a lot for this neat little feature!
I'm wondering what lead you to use 29 as hash multiplier for consecutive
fields in GetHashCode()?
I, for one, believe it should be changed to 42.

class Bar
{
private int a, b, c;
protected bool Equals(Bar bar)
{
if (bar == null) return false;
if (a != bar.a) return false;
if (b != bar.b) return false;
if (c != bar.c) return false;
return true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as Bar);
}
public override int GetHashCode()
{
int result = a;
result = 29*result + b;
result = 29*result + c;
return result;
}
}


2 comments
Comment actions Permalink

Multiplier MUST BE prime number

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Alex Martsynkevich" <evil@whitehouse.gov> wrote in message
news:fpic4v$bff$1@is.intellij.net...

Hi,

>

I just now noticed that resharper allows you to autogenerate Equals and
GetHashCode methods. Thanks a lot for this neat little feature!
I'm wondering what lead you to use 29 as hash multiplier for consecutive
fields in GetHashCode()?
I, for one, believe it should be changed to 42.

>

class Bar
{
private int a, b, c;
protected bool Equals(Bar bar)
{
if (bar == null) return false;
if (a != bar.a) return false;
if (b != bar.b) return false;
if (c != bar.c) return false;
return true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as Bar);
}
public override int GetHashCode()
{
int result = a;
result = 29*result + b;
result = 29*result + c;
return result;
}
}



0
Comment actions Permalink

Eugene,

I wonder if Alex is not being a bit subtle? The number 42 features in Douglas Adams' Hitchhikers Guide To The Galaxy. It goes something like this "if the answer is 42, what is the question?"

0

Please sign in to leave a comment.