Home > Computers and Internet > Should I use a class or should I use a struct?

Should I use a class or should I use a struct?

In .Net the decision to use a struct or to use a class when creating a new type doesn’t come up often. Up until a few months ago I never even considered turning a class into a struct, it seemed like it would come back and bite me later. After seeing some other peoples code and hearing their reasons for why they chose struct’s, I’ve start considering it when I program. For the vast majority of situations, when creating a new type, one should make it a class. But after flushing out the details of usage the opportunity arises to make it a struct.

This opportunity in .Net was created as a way to distinguish .Net from Java. In Java everything is a class. But there are times when have the value on the stack instead of a reference to the value is more performant. One thing I struggle finding online is good guidance of when to use struct’s instead of classes. The best I can find is to not create a struct if it has more than four member fields. The idea being that in a 32-bit process a struct shouldn’t be over 16 bytes. In talking with some friends they also take into consideration how much the instance is going to be passed around. The idea being that if an instance of the type is constantly being copied having a reference to it instead would result in less memory and the overhead of an being object would be worth it.

With all of this in mind I come to my dilemma. I have a FreeCell solver I’ve been writing and in it is the Card type. Right now it’s a class, but it could be a struct. To save on memory there are only 52 instances of the card type instantiated in my application, but there are hundreds of thousands of structures which references these 52 Card instances. If I changed them to a struct the web of references on the heap would be much simpler and the GC would have a lot less work to do. However, where all of the Card arrays are currently only holding references, they would be changed to holding the entire type. Their size would increase by perhaps doubling or tripling. Would the increase in size outweigh the value of having reduced time spent by the GC?

If I’m really worried about the size I could drop one of the fields from the Card class. Right now it has a hash field which is lazily calculated and then stored. If I dropped the field it would need to be calculated every time GetHashCode gets called, and in my application it gets called a lot. On the other hand compilers are pretty smart and should be able to inline the method and make it part of the processor pipeline. So it might not even be measurably different than just returning a stored integer.

So is it worth it? No way to know that for sure without actually doing it and taking measurements. But this situation certainly does make it a possibility. It’s a small class which only holds onto three (possibly two) fields, which are all structs themselves.

Is there anything else to take into consideration when making this decision?

  1. May 12, 2011 at 7:58 pm

    gosh, it took me a while to pick back up the difference between struct and class in .net …

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: