Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal solution for struct with more than 16 bytes

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

like image 856
Chaowlert Chaisrichalermpol Avatar asked Oct 19 '25 01:10

Chaowlert Chaisrichalermpol


1 Answers

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.

like image 190
vboctor Avatar answered Oct 20 '25 17:10

vboctor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!