C# Value Type and Reference Type

According to the Value Type or Reference Type parameters, there are two ways to allocate memory space in C#: either on the stack or the heap.

Here we learned about C# data types and how they are divided into reference and value types with examples.

The value types and reference types in C# will now be covered, along with examples of how the data and memory are held in the same area by the Value Type and Reference Type parameters.

Value Types in C#

A data type is considered a value type in C# if it stores a variable’s value directly on its own memory space. Value types store variable values in stack memory.

The system will utilise the same memory space as variable ‘x’ to store the value ‘123’, for instance, if we define and assign the variable a value like int x = 456.

The following data types are classified as Value Types in the C# programming language.

int float long char bool
byte decimal double enum sbyte
short struct uint enum ushort

 

C# Pass Value Type by Value

If a value type variable is sent from one method to another in C#, the system will make a new duplicate of the variable specifically for the other method. The variable in one method won’t change if we modify the variable in another method.

The example of passing a value type by value in the C# programming language is provided below.

using System;

namespace ConsoleApp3
{
    class Program
    {
         static void Square(int x, int y)
         {
             x = x * x;
             y = y * y;
             Console.WriteLine(x + " " + y);
         }
         static void Main(string[] args)
         {
         int a = 9;
         int b = 5;
         Console.WriteLine(a + " " + b);
         Square(a, b);
         Console.WriteLine(a + " " + b);
         Console.WriteLine("Press Enter Key to Exit..");
         Console.ReadLine();
         }
    }
}

If you look at the example above, you will see that we defined two variables (a, b) in the Main() function. By handing those variables to the Square() method, we are changing them, but such changes have no effect on the variables in the Main() method.

When we run the above mentioned programme, we will obtain the result displayed below.

If you look at the result above, you can see that the variables in the Main() function were unaffected by changes made to the Square() method’s variables.

C# Reference types

Reference Types in C# will have a pointer that points to a different memory region where the data is stored. The variable value won’t be kept in memory directly by Reference Types. Instead, it will save the variable value’s memory location to show where the value is kept.

The system will keep the variable “name” and the variable value “Mango ” along with the memory address of the variable in two different locations, for instance, if we define and assign the value  “Mango ” to the variable.

The following are the different data types that will fall under Reference Type in c# programming language.

 

Pass Reference Type by Value

In C#, the system won’t make a second duplicate of a reference type variable if we pass it from one method to another. Instead, it sends the variable’s address, ensuring that any changes we make to the variable in one method will also be reflected in another.

The C# programming language’s reference type passing by value example is shown below.

using System;

namespace ConsoleApp4
{
    class Person
    {
        public int age;
    }
    class Program
    {
        static void Square(Person x, Person y)
        {
            x.age = x.age * x.age;
            y.age = y.age * y.age;
            Console.WriteLine(x.age + " " + y.age);
        }
        static void Main(string[] args)
        {
        
            Person p1 = new Person();
            Person p2 = new Person();
            p1.age = 9;
            p2.age = 5;
            Console.WriteLine(p1.age + " " + p2.age);
            Square(p1, p2);
            Console.WriteLine(p1.age + " " + p2.age);           
            Console.WriteLine("Press Any Key to Exit..");
            Console.ReadLine();
        }
    }
}

If you look at the example above, you will see that we created a new class called “Person,” created an instance of that class (Person), and assigned values to the variables in the Main() method. We are now changing those variables by passing them to the Square() method, and those changes will be reflected in the Main() method.

The result of running the mentioned application will be as displayed below.

If you look at the result  above, you’ll see that the adjustments we made to the Square() method’s variables also had an impact on the Main() method’s variables.

According to our needs, this is how we can use Value Type and Reference Type in the C# programming language.

Submit a Comment

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

Subscribe

Select Categories