Question

I have a type which I consider use it as struct.

  • It represents single value
  • It is immutable

But the problem is, it has 6 fields of int.

So which solution I should use for this type?

  1. keep using struct?
  2. change to class?
  3. or pack 6 integers into an array of int, so it has only one field

EDIT

Size of struct with 6 integer fields is 24 bytes, which is huge to pass around. Recommend size for struct is not more than 16 bytes

Was it helpful?

Solution

It depends how you are going to use it?

  1. Are you going to allocate a lot of it vs. pass it around a lot?
  2. Is it going to be consumed by 3rd party code? In this case, classes typically give you more flexibility.
  3. Do you want struct vs. class semantics? For example, non-nullable?
  4. Would you benefit from having a couple of pre-created instances of this class that can be re-use to represent special cases? Similar to String.Empty. In this case you would benefit from a class.

It is hard to answer just from the information you provided in your question.

OTHER TIPS

Be careful of boxing. If your struct is going to be consumed by a method which expects an Object, it will be coerced into an object and you'll have a potential performance hit.

Here's a reference that explains it in more detail.

I'd make it a class (#2) and then you wouldn't have to worry about it.

Using an array of six integers (#3) would probably make your code harder to read. A class with descriptive identifiers for each int would be much better.

Without seeing your struct, it's difficult to say anything definitively. But I suspect you should leave this as a struct.

How about a WriteOnce<int[]> ?

I would suggest writing a little benchmark to measure the performance of the different options, this is the only way to know for sure. You may be surprised at the results (I often am).

(I'm assuming that your concern here is performance.)

If the data holder is going to be immutable, the struct-versus-class question will most likely depend upon the average number of references that would exist to each instance. If one has an array of TwentyFourByteStruct[1000], that array will take 24,000 bytes, regardless of whether every element holds a different value, all elements hold the same value, or somewhere in-between. If one has an array of TwentyFourByteClass[1000], that array will take 4,000 or 8,000 bytes (for 32/64-bit systems), and each distinct instance of TwentyFourByteClass which is created will take about 48 bytes. If all of the array elements happen to hold a reference to the same TwentyFourByteClass object, the total will be 4,048 or 8,048 bytes. If all of the array elements hold references to different TwentyFourByteClass objects, the total will be 52,000 or 56,000 bytes.

As for run-time performance, the best performance you can get will generally be passing structures by reference. Passing structures by value will require copying them, which can get expensive for structures larger than 16 bytes (.net includes optimizations for structures 16 bytes or smaller), but the cost of a value type by reference is the same whether it is 1 byte or 16,000 bytes.

In general, when storing more than two pieces of related data I like to make a class that binds them together. Especially if I will be passing them around as a unit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top