473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

value types and GetHashCode

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!
Nov 15 '05 #1
2 2464
Fuzzy <tr******@earthlink.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...

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.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Vottero | last post by:
When a class contains a TimeSpan property, XmlSerializer doesn't work. A TimeSpan property is serialized like: <MySpan /> I've read a number of posts that talk about why this happens and how...
8
by: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
8
by: Dan | last post by:
Hi, I did a test in C# double x1 = 1.0; double x2 = 5.29980882362664E-315; int h1 = x1.GetHashCode(); int h2 = x2.GetHashCode(); It turned out that both h1 and h2 = 1072693248
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
4
by: Joseph Bergevin | last post by:
Why can't I use an int for a key? Doing so evidently doesn't result in unique IDs being generated. I can convert the array into a delimited string, which works fine, but then I have a good deal of...
3
by: MuZZy | last post by:
Hi, Is there any guarantee that MD5 hashing algorithm implementation will not change in the next .NET version unlike what's happened to String.GetHashcode? Thank you, MuZZy
4
by: Andrew Robinson | last post by:
I have a class that has three properties: two of type int and one of type string. Is this the best method when overriding the GetHashCode() ? I am guessing not... any thing better? public...
6
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.