C# Tutorial Beginners - Part 1

Shivani Nalawade
4 min readNov 11, 2020

Welcome back to another story of C# Tutorial for Beginners. This article is all about Stack and Heap data, Garbage collector, Primitive datatypes and Boxing and Unboxing . Your reading this article because you know about programming and you would like to learn more about it. I appreciate it and let’s get started.

Did you ever think how memory allocation/deallocation happens?. Did you ever think about the Stack and heap data structures involvement in program execution?. Let’s discuss all the questions in today’s post.

There are two types of memory allocation in c# :

  1. Stack Allocation
  2. Heap Allocation

Stack Allocation :

The allocation happens on contiguous blocks of memory. The size of memory to be allocated is known to compiler and whenever a function is called, then the memory(variable) is allocated on stack .

And whenever the function call , the memory(variable) is deallocated.

This all happens because of predefined routines in compiler. Programmer does not have to worry about memory allocation and deallocation of stack variables.

Heap Allocation :

Memory is allocated in any random order or memory is allocated during execution of instructions written by programmers.

Allocation and Deallocation happens Manual by programmer .

If a programmer does not handle this memory well, memory leak can happen in the program.

Memory leak occurs when programmers create a memory in heap and forget to delete it.

Example of stack and heap memory allocation

What is Garbage Collection and Why We Need It?

Garbage collector (GC)works on managed heap, which is nothing but a block of memory to store objects.

When you create any object in C#, CLR (common language runtime) allocates memory for the object from heap.

This process is repeated for each and every newly created object, Memory is not un-limited so we need to clean/remove some space for new objects, Here, the concept of garbage collection is introduced, Garbage collector manages allocation and reclaiming of memory.

GC (Garbage collector) used by the application and then makes them free from memory.

C# mainly categorized data types in two types:

  1. Value Type
  2. Reference Type

Definition:

Value Type: A Value Type contains the data inside its own memory allocation. In other words, variables or objects or value types have their own copy of the data.

Reference Type : Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another it doesn’t copy the data.

Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value.

example;

Performance implication of Boxing and Unboxing :

I’m going to explain the essential concepts of boxing and unboxing .

Boxing :

  • The process of Converting a Value type(char, int etc.) to a Reference type(object) is called Boxing.
  • The Value type is always stored in Stack. The Referenced Type is stored in Heap.

Example:

int num = 23; // 23 will assigned to x

object obj = num; // Boxing

Description : First declare a value type variable (num), which is integer type and assigned it with value 23. Now create a references object type (obj) and applied the operation which results in num value type to be copied and stored in object reference type obj as shown in below figure :

Unboxing :

  • The process of converting reference type into the value type is known as Unboxing.

Example :

int num = 23; // value type is int and assigned value 23
Object Obj = num;
//Boxing
int i = (int)Obj; //
Unboxing

  • Description : Declaration a value type variable (num), which is integer type and assigned with integer value 23. Now, create a reference object type (obj).The explicit operation for boxing create an value type integer i . Then the referenced type residing on Heap is copy to stack as shown in below figure :

I hope this guide has been helpful for you 😃!!!

To better understand the concepts of C # please do watch the below video👇. It will resolve your all doubts:

--

--