Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Full details on Struct and Class

Table of Contents
  1. Difference between Struct and Class
  2. Comparison of Struct and Class
  3. Application Memory in struct and classes
  4. Choosing between class and struct

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 values Can 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

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

The post Full details on Struct and Class appeared first on Explore with me!.


This post first appeared on Explore With Me!, please read the originial post: here

Share the post

Full details on Struct and Class

×

Subscribe to Explore With Me!

Get updates delivered right to your inbox!

Thank you for your subscription

×