Introduction: Difference between Stack and Heap memory in C#
In this post, we will learn about the difference between Stack and Heap memory in C#.
In my previous posts I have explained various topics like Difference Between Class And Structure, ASP.NET Core, C# static constructor, Nullables in C# Collections in C#SortedSet in C#.Net,Stack in C#,Queue in C#.Net,LinkedList in C#.Net. Now in this post, I will explain about difference between Stack and Heap memory in C# with appropriate example.
What is Stack?
The stack is an array of memory.
- It follows Last-in, First-out (LIFO) data structure.
- Data or element can be added to and deleted only from the top of the stack.
- Adding a data element at the top of the stack is called pushing the element onto the stack.
- Removing an element from the top of the stack is called popping the element from the stack.
What is Heap Memory?
The heap is an area of memory where chunks are allocated to store certain kinds of data objects or element. Unlike the stack, data can be stored and removed from the heap in any order. Program can store element in the heap, it cannot explicitly delete them. Instead, the CLR’s garbage collector (GC) automatically cleans up orphaned heap objects when it determines that your code can no longer access them.
I hope you got an idea about difference between Stack and Heap memory in C#.
I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.