Let’s talk about Value Type and Reference Type in C#, Today’s topic is a bit complicated so you need to pay more attention to understand. This topic is a basic knowledge of data types. In case you are willing to make your career in programming, you have to know this.
As you already know that the data that you save in a program you save it inside a variable. Based on the type of data you need to declare a variable. For example, you want to save a Name or Address or Email address or any type of string, you need to declare a string-type variable.
In case you want to save a number you need to declare an int, float, double or long. The difference between an int, float, double or long is a topic for another article here you just know that they all save numbers but the only int saves complete numbers like 1, 2, 3, etc. Rest others save 1.2, 2.3, 3.3, etc. This is not the only difference but this is basic different, there is more to this topic.
I hope you have understood what type of variable you need when you want to save a number and what type of variable is required when you want to save string.
Note – Anything that you save inside ” ” is a string.
string number = “1234”; is a string
This is a rule of a string that it takes the data inside ” “, so it does not even matter what you save in a string between Double Quotes (” “), it will be considered as a string.
Value Types Variable

There are two types of memories, value type, and reference type. The value type of variable gets saved inside stack memory. Due to the difference in the data types, the developers of C# decided to save both types of variables on the different parts of the memory. Whenever we save a value inside a value type variable, the value gets saved in the memory directly. Just like you can see in the image above. The variable “i” is holding an address (0x98329) in the memory and at that address, the value “200” is saved.
Below is the table, you can see the value types variables with a default value and their ranges.
Data Type | Size | Range | Default Value |
int | 32 bit Signed Integer | from -2147483648 to 2147483647 | 0 |
long | 64 bit signed integer | from -9223372036854775808 to 9,223372036854775807 | 0L |
float | 32 bit single precision floating point | from -3.4 x 1038 to + 3.4 x 1038 | 0.0F |
double | 64 bit double precision floating point | from (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 | 0.0D |
decimal | 128 bit precise decimal values | from (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 | 0.0M |
short | 16 bit signed integer | from -32768 to 32767 | 0 |
uint | 32 bit unsigned integer | from 0 to 4,294,967,295 | 0 |
ulong | 64 bit unsigned integer | from 0 to 18,446,744,073,709,551,615 | 0 |
ushort | 16 bit unsigned integer | from 0 to 65,535 | 0 |
byte | 8 bit unsigned integer | from 0 to 255 | 0 |
bool | Boolean Value Type | True or False | False |
char | 16 bit Unicode character | from U +0000 to U +ffff | U+0000 |
sbyte | 8 bit signed integer | from -128 to 127 | 0 |
enum | Depends on the processor type | User Defined | (E)0 where E is the enum identifier |
Reference Type Variable

The reference type variable does not hold the data directly, the variable that you declare holds an address in the memory and at that memory address, there will be another address in the memory. Now, this is the address where the data is actually saved.
You can see the visual representation of the reference type variable. The variable “i” is a reference type variable that is holding a memory address (0x98329), when reaching to that memory address there is another memory address (0x898793). Now, this is the address where the real data string “Welcome” is stored.
Reference Data Types
- Class
- Interface
- Delegate
- Dynamic
- Object
- String
Why do we have Reference Type
You must be thinking about why Microsoft created Value Type and Reference Type. If we want to understand why we have Value Type and Reference Type, we need to understand the difference between them. The main difference occurs when we pass the reference of variable1 to variable2 and change the value of Variable1. How will it affect the value saved inside variable2?
Don’t worry if you did not understand it. Look at the program below and you will understand.
Program Example for Reference Type
using System;
namespace Namespace
{
class Program
{
class Employee
{
public int Salary { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
Employee e = new Employee();
e.Name = "John";
e.Salary = 10000;
Employee e2 = e;
e.Salary = 5000;
e.Name = "Kenith";
Console.WriteLine("Name - {0}",e2.Name);
Console.WriteLine("Salary - {0}",e2.Salary);
Console.ReadLine();
}
}
}
Program Output

Program Explanation
In the above program, you can see we have created a class called Employee. There are two properties (Name and Salary) inside that Employee class. In the main method, we have created an instance “e” of Employee class and initialized (Filled data or Assigned values) the properties Name and Salary.
Later on, we created a variable “e2” of type Employee and saved the reference of “e” inside it. The properties of “e” those were already initialized are now passed on to “e2” because “e2” holds the reference of “e”.
At the next stage, we have changed the values of the properties of “e”. Due to the reference type, the values saved inside “e2” will also be changed because “e2” only holds the memory address of “e”. The “e2” does not hold the properties and their values of “e”.
Program to Understand Value Types
using System;
namespace Namespace
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = a;
a = 20;
Console.WriteLine("The Value of b is - {0}",b);
Console.ReadLine();
}
}
}
The output of the Program

Program Explanation
The above program is very simple to understand. The situation is exactly the same as the reference type but the output of different. The value of “a” has been changed but the value of “b” is still the same. This has happened due to value types.
Whenever the reference of a value type variable is passed on to another variable, a fresh copy of the value is passed on, not the reference (address). The new variable holds a fresh copy of the value. In the above example when “b = a” happened, a fresh copy of 10 was passed on to b. After this “b=a” point in the program, if the value of “a” changes 100 times, it will not affect the value of “b” because it holds its own value now.
I hope, you have understood the concept. In case of any confusion of any mistake in the article, feel free to comment. I would appreciate that.
Take care Guys!