Professional Documents
Culture Documents
Value Type
In C#, Value types are of two types, built-in and user-defined.
Built in value
type: int , long , float , double , byte , decimal , char , bool , short ,
User defined value type: struct , enum
In C# all the value types are derived from System.ValueType
A value type in C# does not hold a reference, rather it holds the actual value in
its own memory allocation in the stack.
For example, if we assigned a value to the integer variable int x = 100, then the
same memory space of variable x will be used to store the value 100.
Memory allocation of a value type in stack memory.
In C# value types variable has its own copy of the data. So, when we do
assignment and change the value of one variable it will not affect the original
variable (except in the case of in, ref and out parameter )
Let’s declare the second int variable ‘y’ and assign the value of the first
variable ‘x’ to it. Here the value will be copy from the variable ‘x’ to ‘y’.
Now in the stack, two separate memory spaces will be created by CLR (Common
Language Runtime) for x and y which are completely independent of each other
and both of them hold the same value 100.
Now let’s increment the value of the variable ‘y’ by 1. Here we will notice that the
value of the variable ‘x’ will not be changed and remain the same because both
variables have separate memory allocation.
That’s why changes in one variable will not affect the other variable. These kinds
of data types are the Value Types in C# .
The following are the example.
r
Example of a value type in C#
1
2 using System;
3 class Program
4 {
static void Main()
5 {
6 // Value types
7 int x = 100;
8 int y = x;
y++;
9
10
Console.WriteLine($"x = {x} y = {y}");
11 Console.ReadLine();
12 }
13 // Output: x = 100 y = 101
14 }
15
Let’s execute the above program and see the result.
The output of the above program.
In the above result, we can notice that the value of x remains the same.
Of course, because both of the variables ‘x’ and ‘y’ belong to the different
memory locations and they have nothing to do with each other.
Note: It’s a misconception that value types are always allocated on the stack, but
it is not true.
The value type will only be allocated on the stack when the variable is a
temporary or local variable or when it is used directly inside a method.
However, if we declare a variable of the value type at the class level or inside
another reference type it will get allocated in the GC heap memory.
A value type variable gets free from the memory automatically once it goes out
of the scope. There is no role of the Garbage collector to free the memory.
Reference type
In C# Reference types are of two types, built-in and user-defined.
Built in reference type: object , string , dynamic
User defined reference type: Class , Interface , Array , delegate
In C# all the reference types are derived from System.Object
A reference type does not store its value directly, instead it contains pointer to a
memory location that holds the actual data in the managed heap.
For the reference type, the reference of the actual object is used to store in the
stack memory and the object itself in the heap.
In the case of reference type, two variables can reference the same object in the
heap memory. So, Operations on one variable can affect the object referenced
by the other variable.
Example of reference type in C#
Let’s understand the reference type by using the following C# program.
1 using System;
namespace ReferenceType
2 {
3 // User class
4 class User
5 {
6 // Field
public int age = 10;
7 }
8
9
10 class Program
11 {
12
13 static void Main(string[] args)
14 {
// Creating object of the 'User' class.
15 User user1 = new User();
16
17 // Assign object user1 to the new object user2
18 User user2 = user1;
19
20 // Print the value of object a and b
21 Console.WriteLine($"user1.age = {user1.age} user2.age ={user2.age}");
//Changed value of variable b
22 user2.age = 20;
23
24 // Print the value of object a and b
25 Console.WriteLine($"user1.age = {user1.age} user2.age ={user2.age}");
26 }
}
27
}
28
29
30
Let’s execute the above program to see the result.