value types and GetHashCode

F

Fuzzy

I have defined a struct in my application that contains 3 integers
that maintain state information. I have a lot of these structs in
time-critical portions of my app, so they must be as fast as possible.
I also log previous values in arrays, so data size can't be ignored.

The data is such that I can implement value semantics by implementing
IComparable.CompareTo and overriding all the comparison operators as
well as Object.Equals(object o).

Since I have overriden the equality operators, the compiler keeps
suggesting I override Object.GetHashCode(). From reading the docs, I
realize GetHashCode() must return the same value for any given
instance and should be based on an immutable data member. However, my
value type is NOT immutable; I am constantly modifying values as state
changes and don't want the overhead of copying the struct every time I
need to change any of the 3 int fields.

Do I really need to be concerned about this? Can I accept the
default implementation of Object.GetHashCode() even though my struct
is not an object unless it is boxed? I really don't want to add a new
field just for HashCode support because that increases my data size by
33%!

I'm not planning on using this struct in a Hashtable or in collections
at this point, but that may happen in the future.

I would appreciate any insights into this problem!
 
J

Jon Skeet [C# MVP]

Fuzzy said:
I have defined a struct in my application that contains 3 integers
that maintain state information. I have a lot of these structs in
time-critical portions of my app, so they must be as fast as possible.
I also log previous values in arrays, so data size can't be ignored.

The data is such that I can implement value semantics by implementing
IComparable.CompareTo and overriding all the comparison operators as
well as Object.Equals(object o).

Since I have overriden the equality operators, the compiler keeps
suggesting I override Object.GetHashCode(). From reading the docs, I
realize GetHashCode() must return the same value for any given
instance and should be based on an immutable data member. However, my
value type is NOT immutable; I am constantly modifying values as state
changes and don't want the overhead of copying the struct every time I
need to change any of the 3 int fields.

Do I really need to be concerned about this? Can I accept the
default implementation of Object.GetHashCode() even though my struct
is not an object unless it is boxed? I really don't want to add a new
field just for HashCode support because that increases my data size by
33%!

Value types don't really *have* instances, in a way - it's not like
there can be two references to the same instance, there would just be
two completely independent values.

Basically, you can override GetHashCode just based on your values.
 
F

Fuzzy

Jon Skeet said:
Value types don't really *have* instances, in a way - it's not like
there can be two references to the same instance, there would just be
two completely independent values.

Basically, you can override GetHashCode just based on your values.

Yes, that is obvious, and I realized it soon after I posted! Anything
that goes into a HashTable is a copy *anyway*, so what difference does
it make!?!

Thanks for taking the time to respond.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top