Memory Types

Let's discuss memory types provided in C#.

Heap and stack

When a variable is declared, a portion of memory is allocated in RAM. This portion of memory contains the variable’s name, data type, and value. Depending on the data type, the portion of memory allocated will be either stack or heap memory.

  • The stack is used to track local variables and the program’s state.
  • The heap is used to store data that can be accessed anytime and from anywhere in the program.

Data types are either value or reference types. The most common reference types are strings, arrays, and objects that are on the heap; everything else is a value type on the stack.

Get hands-on with 1200+ tech skills courses.