Value Type & Reference Type
Application memory is mainly divided into two memory
component . Heap and Stack .
Heap is a separate area of memory for holding reusable
objects .Actual object is allocated in Heap .A variable containing pointer to
an object is allocated on stack , when call by stack actually return the memory
address of the objects.
Value Type : Int/Bool/Struct /Enum
Reference Type :Class/array/interface/delegate
Value Type
- Value Type are stored on stack
- Value Type contains actual value
- Value Type cannot contain null values.
- Value type is popped on its own from stack when they go out of scope.
- For Value Type Memory is allocated at compile time
Reference Type
- Reference Type are stored on heap
- Reference Type contains reference to a value
- Reference Type can contain null values.
- Reference Type required garbage collector to free memory.
- Reference Type memory is allocated at run time
Stack
- very fast access
- local variables only
- variables cannot be resized
- It is a Last-in, First-out (LIFO) data structure
- Static memory allocation
Heap
- No limit on memory size
- Variables can be resized
- Fist in First Out
- Dynamic memory allocation
Example of Stack
int a=100;
string ="C# program”;
int a=5+6;
Exaple of Heap
{
int myInt = 0;
string myString = "Something";
}
class Program
{
static void Main(string[] args)
{
MyClass m = new MyClass();
}
}
No comments:
Post a Comment