Understanding difference between Class and Struct

Classes and Structs are key entities of c# programming language and understanding the difference between these two also very important. In this blog post, I am going to cover the difference between class and struct. What you need to consider when you design an application? and how you will are going to choose the right datatype for your application? everything depends on the understanding of this particular concepts.

So we are going to look at the below points,

  1. Memory Allocation – Cost of Memory allocation
  2. Memory Usage
  3. Copying
  4. Pass by value/ Pass by reference
  5. When you should use Struct?
  6. When you should avoid Struct?

Memory allocation:  In DOT.NET Framework memory management is automatically handled by common language runtime, even though as a developer you need to consider understanding memory allocation is very important. There are two primary places your data will be stored when your program executes and they are Stacks and Heaps.

Structs are value types and classes are reference types.

All the value types are getting stored in Stack. Operation wise getting the values from the stack is much faster than Heaps. Stack memory is de allocated when the stack unwinds or Garbage collector collects the unused variables.

All the reference types will be allocated in Heaps, and memory management is automatically handled through garbage collector (CLR).

Cost of memory allocation – Value types vs Reference Types :   When you think about allocation and free up the memory, cost involved in memory management of value type is much cheaper than reference type.

Why Memory allocation for the value type is better than reference types?  – Let us consider a scenario where you have an two array one of the type value and another of the type of reference.

  • Value type array stored in-line in the memory, hence accessing the value type is much easier when compared with reference types.
  • Reference type array stored out-of-line, for example the array doesn’t hold the reference type  in the array, instead it holds the reference type variables.

This is the main reason why the value type memory allocation is much faster than reference types.
Memory Usage:  Boxing and unboxing are important when you want to thinking of converting value type and reference type.

  • Boxing is a process of converting a value type to a reference type.
  • Unboxing is a process of converting reference type to value type.

Box are “object” which allocated in a heap for which memory management handled by the garbage collector. Too much of boxing and Un boxing conversion creates negative impact on Heap, Garbage collection, Application performance

But in contrast, no such boxing or unboxing happens during the cast operations in reference.

3.Copying

  • Reference types copies the reference, instead of copying the whole variable
  • Value types copies the complete variable

So assignment of large reference types is much cheaper than the assignment of value types.

4.Pass by Reference / Pass by value

  • Reference types are passed by reference, changes to the instance of the reference types affects all the reference points to it.
  • Value types are passed by value, changes to the instance of value type do not make any change on any of its copy.

Use structs when

  • Small
  • Commonly short lived
  • Commonly embedded in other instance

Avoid Structs when

  • It logically represent a single value like integer, double, float, etc
  • size under 16 bytes
  • it should not be boxed frequently
  • it should be immutable.

so using struct or class is completely depend on the design and requirement of your application. please post your thought below in the comment section.

0 comments… add one

Leave a Comment