Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is best for data store Struct/Classes?

Tags:

c#

class

struct

We have seen lots of discussion in SO regarding the class vs struct in c#. Mostly ended with conclusions saying its a heap/stack memory allocation. And recommending to use structs in small data structures.

Now I have a situation to decide the simple data store among these two choices. Currenlty in our application we have thousands of classes, just acts as simple data stores (only exposed public fields) and they passed among different modules and services.

As per my understanding, I felt it's better to move ahead with struct instead classes for the performance reasons. Because these are simple data structures only act as data stores.

Before proceeding with this, I need some expert advice from the people who have experienced this struggle.

  • is my understanding correct?
  • I have seen most ORMs have classes as data stores. So I doubt there should a reason to go ahead with classes instead structs. what would that be?
like image 382
RameshVel Avatar asked Dec 23 '09 07:12

RameshVel


People also ask

Which is better struct or class?

There is no difference between classes and structs. Structs are classes; only default access is flipped from private to public.

Is struct more efficient than class?

The only difference between these two methods is that the one allocates classes, and the other allocates structs. MeasureTestC allocates structs and runs in only 17 milliseconds which is 8.6 times faster than MeasureTestB which allocates classes! That's quite a difference!

Do classes use more memory than structs?

"is known that struct objects consume less memory than class objects because struct object does not need an additional memory location to store the memory address of the object created from the new operator." Yes, that's indeed a difference.


2 Answers

I would make the choice based on the following criteria

  • reference type vs value type semantics. If 2 objects are only equal if they are the same object, it indicates reference type semantics => class. If the value of its members defines equality (e.g. 2 DateTimes are equal if both represent the same point in time even if they are 2 distinct objects), value type semantics => struct
  • Memory footprint of the object. If the object is huge and frequently allocated, making it a struct would consume the stack much faster, hence I'd rather have it as a class. On the contrary, I'd rather avoid the GC penalty for small value types; hence make them a struct.
  • can you make the object immutable? I find structs great for 'value objects' - from the DDD book.
  • Would you face some boxing-unboxing penalty based on the usage of this object? If yes, go for class.
like image 132
Gishu Avatar answered Oct 12 '22 05:10

Gishu


A pretty cool, not so well known advantage of Structs over Classes is that there is an automatic implementation of GetHashcode and Equals in structs.
That's pretty useful when keys are required for dictionaries

The struct implementation of GetHashcode and Equals is based on the binary content of the struct instances + reflection for the reference members (like String members and other instances of classes)

So the following code works for GethashCode/Equals :

public struct Person {     public DateTime Birthday { get; set; }     public int Age{ get; set; }     public String Firstname { get; set; } } class Program {     static void Main(string[] args)     {         Person p1 = new Person { Age = 44, Birthday = new DateTime(1971, 5, 24), Firstname = "Emmanuel" };         Person p2 = new Person { Age = 44, Birthday = new DateTime(1971, 5, 24), Firstname = "Emmanuel" };         Debug.Assert(p1.Equals(p2));         Debug.Assert(p1.GetHashCode() == p2.GetHashCode());     } } 

Both assertions succeed when Person is a struct Both assertions fail if Person is a class instead of a struct

Reference : https://msdn.microsoft.com/en-Us/library/2dts52z7%28v=vs.110%29.aspx

Regards, best coding

like image 39
Emmanuel DURIN Avatar answered Oct 12 '22 05:10

Emmanuel DURIN