1

I know this is a frequent question, but I read a lot about it and still can't decide which is the better choice for my case:

I need to design a data structure which have relatively many primitive fields, say 'Person' which contains a lot of information about the person. The underlying framework is MS Orleans, and there are expected to be hundreds of thousands 'Person' instances in memory at every given time (on each machine). At first I thought struct is the better choice since there will be less GC pressure and memory overhead. But as I began to implement the design it turned out to be a pain - 'Person' has 4 nested structs with ~8 fields each (including arrays), and I spent too long implementing IEquatable for each one, not mention that every minor change in one of the structures requires refactoring IEquatable implementation and the ctors.

But then I thought that since 'Person' objects will not be short-lived objects, they will probably get promoted to generation 1 or 2 so maybe GC pressure is not that big of a deal. As for memory overhead (of classes) - I can live with that...

So, can anyone share their thoughts about this case? MSDN suggest not to use structs for objects larger than 32 bytes, which my object easily exceeds.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
shay__
  • 3,815
  • 17
  • 34
  • 2
    It's not clear what `IEquatable` has to do with this. You may well want to implement it with a class as well as a struct - and you don't *have* to do it for structs either, although not wanting to is probably a symptom that it shouldn't be a struct. (It doesn't sound like a natural self-contained value to me...) – Jon Skeet Jul 05 '15 at 07:18
  • 1
    Value types are good when you want to describe a structure which is itself, some sort self contained value. Also, the copy semantics of structs can be confusing to people who aren't too familiar with the differences in the framework. And, considering you have arrays that will live on the heap anyway, i'd go with a class. – Yuval Itzchakov Jul 05 '15 at 07:19
  • @JonSkeet - I guess you're right (duh), the object is too complicated to be a struct.. A lesson learned the hard way ;) – shay__ Jul 05 '15 at 07:31
  • @YuvalItzchakov - this is not the first time you answer my questions, Toda :) – shay__ Jul 05 '15 at 07:32
  • @shay__ You welcome ;) – Yuval Itzchakov Jul 05 '15 at 07:36
  • 1
    There are three schools: 1: Use struct if the size is less than 12 byte. 2) use struct to hold data that won't change, clases for volatile data and abilities. 3) __use classes when you have to ask or even think about it..__ – TaW Jul 05 '15 at 08:08
  • Class and structure types should avoid exposing array types as public members except in cases where the clear purpose is to *identify* rather than *encapsulate* an array. Further, structures should only encapsulate data in arrays (even private ones) if they can guarantee that everything that will ever be stored to those arrays will be stored before references to the array are stored into structure fields. – supercat Jul 06 '15 at 19:49

0 Answers0