Value vs. Reference Types

Understand the distinction between value and reference types to control how data is stored in memory and correctly manage side effects when passing arguments to methods.

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: