Search⌘ K
AI Features

Value vs. Reference Types

Explore the fundamental differences between value and reference types in C#. Understand how value types store data directly on the stack, while reference types hold addresses to objects on the heap. Learn how passing these types as method parameters affects data and references, including the use of the ref keyword.

We’ve learned about the primitive types and have also worked with classes and structs. All data types in .NET are divided into two categories: value types and reference types.

Value types

When we create a variable of a value type, a place in memory is allocated for that value and the slot contains the actual data.

Note: All primitive types like int, double, bool, float, and char are examples of value types. When we create a struct, we create a value type. Primitive types are essentially structs.

Consider the following example:

int age = 25;

The age variable represents a specific location in memory, and that location contains the actual value 25.

Value type variable
Value type variable

Another important feature of value types is that they’re stored in the stack. ...