Warning in a struct: "Non-readonly field referenced in GetHashcode"

I understand why this warning appears for reference types[1], but not why it appears for value types. If a struct is passed into a Dictionary object to be used as a key, for example, a copy is made, and any internals which are used to compute the hash code will be effectively immutable, regardless of whether they are marked readonly. Is there another case I'm not thinking about where this can lead to a bug? Or is Resharper's warning in this case more of a general hygiene thing than warning of an incipient bug?

[1] https://resharper-support.jetbrains.com/hc/en-us/community/posts/206354929-Non-readonly-field-referenced-in-GetHashcode

1 comment
Comment actions Permalink

Think I figured it out. If a struct's field is a reference type, shared, mutable and used to calculate the struct's hash code, then shenanigans could ensue.

Would be nice if Resharper were smart enough to notice in my case that the fields being used to calculate the hash code are primitives and so aren't subject to this issue since they live inside a struct, but oh well.


Please sign in to leave a comment.