Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Value Type vs Reference Type in C#
Value type and reference type are two fundamental categories of data types in C#. Understanding their differences is crucial for memory management and program behavior. Value types store data directly, while reference types store a reference to the memory location where the data is stored.
Syntax
Following is the syntax for declaring value type variables −
int number = 10; char letter = 'A'; bool flag = true;
Following is the syntax for declaring reference type variables −
string text = "Hello"; object obj = new object(); int[] array = new int[5];
Value Type
Value type variables store data directly in memory. They are derived from the class System.ValueType. When you declare a value type, the system allocates memory to store the actual value.
Value type variables are stored on the stack, which provides fast access and automatic memory cleanup when the variable goes out of scope.
Example
using System;
class Program {
public static void Main() {
int a = 10;
int b = a; // Copy the value
Console.WriteLine("Before modification:");
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
b = 20; // Only b changes
Console.WriteLine("After modifying b:");
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
}
}
The output of the above code is −
Before modification: a = 10 b = 10 After modifying b: a = 10 b = 20
Reference Type
Reference type variables store a reference (memory address) to the location where the actual data is stored. Multiple variables can refer to the same memory location. If the data is changed through one variable, all other variables referencing the same location will reflect this change.
Reference type variables are stored on the heap, with the reference itself stored on the stack.
Example
using System;
class Program {
public static void Main() {
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1; // Copy the reference
Console.WriteLine("Before modification:");
Console.WriteLine("arr1[0] = " + arr1[0]);
Console.WriteLine("arr2[0] = " + arr2[0]);
arr2[0] = 10; // Modifies the same array
Console.WriteLine("After modifying arr2[0]:");
Console.WriteLine("arr1[0] = " + arr1[0]);
Console.WriteLine("arr2[0] = " + arr2[0]);
}
}
The output of the above code is −
Before modification: arr1[0] = 1 arr2[0] = 1 After modifying arr2[0]: arr1[0] = 10 arr2[0] = 10
Common Value Types and Reference Types
| Value Types | Reference Types |
|---|---|
| int, float, double, decimal | string, object, dynamic |
| char, bool | Arrays (int[], string[]) |
| byte, sbyte, short, ushort | Classes, Interfaces |
| long, ulong, uint | Delegates |
| struct, enum | Collections (List, Dictionary) |
Key Differences
| Aspect | Value Type | Reference Type |
|---|---|---|
| Storage Location | Stack | Heap (reference on stack) |
| Assignment Behavior | Copies the actual value | Copies the reference |
| Default Value | Zero or false | null |
| Memory Management | Automatic cleanup | Garbage collection |
Conclusion
Value types store data directly and are independent copies when assigned, while reference types store memory addresses and share the same object when assigned. Understanding this distinction is essential for proper memory management and avoiding unexpected behavior in C# applications.
