C#: 11-Value And Reference Types

Value type

int is value type

int x = 1;
int x;
int x = 0;
int x = null // throws compile error


 

Reference type

var employee = new employee();

 

Memory Considerations

using new, consumes memory

once variable is no longer used, the memory is still consumed, but no accessible

Memory Leaks occur when this happens

Garbage Collector will cleanup periodically, but this also consumes CPU power to manage

So writing lean code is important for larger or load-intensive applications

Stack

Memory management here is simpler

int x;

Heap ( with Garbage Collector )

More complex memory management

new Employee()
Tags