You are on page 1of 12

Memory Management

The Stack & The Heap


LIFO & FIFO

A stack is a LIFO

A queue is a FIFO
Value types and reference types
Value types are placed on the Stack (and sometimes on the Heap)

Reference types are placed on the Heap (and on the stack we have a pointer to
them)
Value types
Enums

DateTime

Int, Double, Float, Short, Long

Bool

Char

All structures created by our application


Enums
a set of named constants

Example:

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};


Structs
is suitable for representing lightweight objects such

Example
Reference types
String

Object

All classes created by our application


How our code runs in terms of Memory
Function calls are placed on stack

Value type parameters are copied on the stack

Reference type parameters have pointers on the stack


Walk through the code
Walk with value types
Walk through the code
Walk with value & reference types
Boxing and Unboxing
In .NET everything inherits from Object (even value types)

Example and what we have on the Stack and on the Heap


Mutable and Immutable objects
Mutable vs Immutable objects

String is immutable

StringBuilder is mutable

Example how to create a string (class Author)

You might also like