Value Type vs Reference Type in C# – Part 1

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

Value Type vs Reference Type in C# - Part 1 1

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 TypeSizeRangeDefault Value
int32 bit Signed Integerfrom -2147483648 to 21474836470
long64 bit signed integerfrom -9223372036854775808 to 9,2233720368547758070L
float32 bit single precision floating pointfrom -3.4 x 1038 to + 3.4 x 10380.0F
double64 bit double precision floating pointfrom (+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
decimal128 bit precise decimal valuesfrom (-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
short16 bit signed integerfrom -32768 to 327670
uint32 bit unsigned integerfrom 0 to 4,294,967,2950
ulong64 bit unsigned integerfrom 0 to 18,446,744,073,709,551,6150
ushort16 bit unsigned integerfrom 0 to 65,5350
byte8 bit unsigned integerfrom 0 to 2550
boolBoolean Value TypeTrue or FalseFalse
char16 bit Unicode characterfrom U +0000 to U +ffffU+0000
sbyte8 bit signed integerfrom -128 to 1270
enumDepends on the processor typeUser Defined(E)0 where E is the enum identifier

Reference Type Variable

Value Type vs Reference Type in C# - Part 1 2

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

  1. Class
  2. Interface
  3. Delegate
  4. Dynamic
  5. Object
  6. 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

Reference Type 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

Value Type Program Output

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!

Share your love
Nadeem
Nadeem

Leave a Reply

Your email address will not be published. Required fields are marked *