Full details on Struct and Class

Full details on Struct and Class

Difference between Struct and Class

StructClass
Struct are value types.Classes are of reference types.
Struct are stored on the stack.Classes are stored on the heap.
Being a value type they hold their value in memory when they are declared.In case of classes, the type holds a reference to an object memory.
Value types are destroyed immediately after the scope is lost.In reference type only the variable gets destroyed after the scope id lost. Object is destroyed later by Garbage Collector.
When you copy struct into another struct, a new copy of that struct gets created modified of one struct won’t affect the value of the other struct.When you copy a class into another class, it only copies the reference variable.
Structs can not have destructors.Classes can have destructors
Structs can not have explicit parameterless constructorsClasses can have explicit parameterless constructors
1 Per thread1 per application
Can’t have null valuesCan have null values

Comparison of Struct and Class

StructClass
TypeValue-typeReference-type
WhereOn stack / Inline in containing typeOn Heap
DeallocationStack unwinds / containing type gets deallocatedGarbage Collected
ArraysInline, elements are the actual instances of the value typeOut of line, elements are just references to instances of the reference type residing on the heap
Al-Del CostCheap allocation-deallocationExpensive allocation-deallocation
Memory usageBoxed when cast to a reference type or one of the interfaces they implement,
Unboxed when cast back to value type
(Negative impact because boxes are objects that are allocated on the heap and are garbage-collected)
No boxing-unboxing
AssignmentsCopy entire dataCopy the reference
Change to an instanceDoes not affect any of its copiesAffect all references pointing to the instance
MutabilityShould be immutableMutable
PopulationIn some situationsMajority of types in a framework should be classes
LifetimeShort-livedLong-lived
DestructorCannot haveCan have
InheritanceOnly from an interfaceFull support
PolymorphismNoYes
SealedYesWhen have sealed keyword
ConstructorCan not have explicit parameterless constructorsAny constructor
Null-assignmentsWhen marked with nullable question markYes)
AbstractNoWhen have abstract keyword
Member Access Modifierspublic, private, internalpublic, protected, internal, protected internal, private protected

Application Memory in struct and classes

Struct vs Class
Struct vs Class

Image reference : Stackoverflow

Choosing between class and struct

As a rule of thumb, the majority of types in a framework should be classes. There are, however, some situations in which the characteristics of a value type make it more appropriate to use structs.

✔️ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

❌ AVOID defining a struct unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.).
  • It has an instance size under 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

In all other cases, you should define your types as classes.

reference : https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/choosing-between-class-and-struct

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: