开发者

Is it safe to rely on Func.GetHashCode for a set of Func definitions?

I have a number of Func definitions (like 30) that checks for things, for instance:

IsLayerEnabled
IsEffectEnabled
...

I want to hash these in a Dictionary. 开发者_如何学CIs it safe to just use:

IsLayerEnabled.GetHashCode()
etc

for each Func definition?

Or would they be the same hash value for each?


They will provide (typically) a different hash code for each delegate. However, Delegate.GetHashCode (which is what generates a hash for any delegate) does not prohibit hash collisions.

That being said, Dictionary<T,U> handles hash collisions very well, and with 30 elements, you will be unlikely to have any real issues.


The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

You can use it for sure. I dont see any problem in that. Thats why its there.

Edit

But yeah, The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜