Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why GetHashCode is not a property like HashCode in .NET

Tags:

c#

.net

hashcode

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

like image 268
Joan Venge Avatar asked Feb 11 '09 20:02

Joan Venge


2 Answers

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.

like image 144
MichaelGG Avatar answered Nov 15 '22 22:11

MichaelGG


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.

like image 40
Scott Weinstein Avatar answered Nov 16 '22 00:11

Scott Weinstein