tags:

views:

601

answers:

7

Why GetHashCode is not a property like HashCode in .NET?

+15  A: 

Probably because it requires computation, and exposing it as a propery might imply that the hashcode is already available for free.

Edit: Guidelines on this: Properties versus Methods

"The operation is expensive enough that you want to communicate to the user that they should consider caching the result."

Perhaps GetHashCode is expensive enough in some cases.

MichaelGG
Thanks, is there a guideline for not having properties that requires computation? The reason I ask is, I have seen lots that computes when you access the property.
Joan Venge
@Joan, sometimes it can be slow to generate a good Hashcode
Ian Ringrose
+1  A: 

The hash must be calculated, and...

The GetHashCode method can be overridden by a derived type. Value types must override this method to provide a hash function that is appropriate for that type and to provide a useful distribution in a hash table. For best results, the hash code must be based on the value of an instance field or property instead of a static field or property.

Source: MSDN

John Rasch
Thanks, but properties can be overridden too, right?
Joan Venge
+1  A: 

I don't think there's any good reason. Any implemention of GetHashCode should be fast enought to put into a property. That said, there are plenty of design flaws in the .Net framework, some small, some serious. This seems like a small one.

Scott Weinstein
Based on the guidelines specified in the Framework Design Guidelines, if the computation is "expensive" it should be a method not a property. Since hashcode computation is a complete unknown for custom objects as far as the Framework is concerned it's better as a function.
Scott Dorman
A: 

properties should only be used if the computation behind them is really fast or cached

besides most of the time the only logic in properties should be validation

LDomagala
A: 

You have to remember that the .NET Framework is designed to be accessed by a wide variety of languages.

In theory you could create a compiler that is incapable of correctly overriding properties. While that would make for a pretty crappy compiler, it would not necessarily be illegal. (Remember properties are just methods with some meta data)

Guvante
You could also make a compiler that can't override methods. And plenty of other .NET interfaces require overring a property.
MichaelGG
Plenty of interfaces require overriding a property, but Object is the grandfather of everything, why not keep it simple?
Guvante
+1  A: 

Besides that a property is nothing else than a getter and a setter method, from a design perspective a property should never contain any computations other than initializing or validation, eg:

private object _obj;
public object Obj
{
  get
  {
    if(_obj == null)
    {
      _obj = new object();
    }
    return _obj;
  }
  set
  {
    if(value == badvalue)
    {
      throw new ArgumentException("value");
    }
    _obj = value;
  }
}

GetHashCode() does not contain extensive computations, but it could contain such long running operations (just from the fact that it could compute the hashcode of an object in a complex manner), this is why its a method instead of a property.

haze4real
+1  A: 

Often it is not possible to define a HashCode for a class that makes since:

e.g. the objects of the class don’t have a well defined concept of identity.

Therefore it is common to make the GetHashCode() method throw a NotImplementedException. This would course all sort of problem if HashCode was a property, as most people (and debuggers) assume it is always valid to get the value of a property

Ian Ringrose