Pass Object by Reference in C#

  1. Pass Variables by Value to a Function in C
  2. Pass Variables by Reference to a Function in C
  3. Pass Objects by Reference to a Function in C

This tutorial will discuss the methods of passing an object to a function in C#.

Pass Variables by Value to a Function in C

All the value type variables are passed by value by default in C#. Suppose we pass a variable by value to a function and modify that variable in the function. In that case, there is no effect on the value of the variable passed in the function’s arguments. What happens here is that a separate copy of the passed variable is created in the memory, and all the operations are performed on that particular copy. The following code example shows us how we can pass a value type variable by value in C#.

using System;

namespace pass_object_by_reference
{
    class Program
    {
        static void method1(string value)
        {
            value = "something";
            Console.WriteLine("New value = {0}",value);
        }

        static void Main(string[] args)
        {
            string valueType = "Nothing";
            method1(valueType);
            Console.WriteLine("Original Value = {0}",valueType);
        }
    }
}

Output:

New value = something
Original Value = Nothing

In the above code, we passed the string variable valueType by value to the method1() function that assigns a new value to the passed variable. Two different copies of variables having the same value are created in the memory when we pass the valueType variable by value to the method1() function. And then, the second copy of the variable gets modified to something value. This second copy is only accessible inside the method1() function. Any change to this second value does not change the original variable valueType.

Pass Variables by Reference to a Function in C

We can also pass a value type variable by reference with the ref keyword in C#. The ref keyword specifies that a value is passed by reference to a function. With this approach, there will only exist one copy of the variable in the memory, and a separate pointer will be passed to the function that points to the value of the variable. The following code example shows us how to pass a value type variable by reference with the ref keyword in C#.

using System;

namespace pass_object_by_reference
{
    class Program
    {
        static void method1(ref string value)
        {
            value = "something";
            Console.WriteLine("New value = {0}",value);
        }

        static void Main(string[] args)
        {
            string valueType = "Nothing";
            method1(ref valueType);
            Console.WriteLine("Original Value = {0}",valueType);
        }
    }
}

Output:

New value = something
Original Value = something

In the above code, we passed the value type variable valueType by reference to the method1() function with the ref keyword in C#. The method1() function takes a reference to the valueType variable as an argument and modifies the value of the valueType variable to something. This time, the original value gets modified.

Pass Objects by Reference to a Function in C

By default, all the reference type variables like class instances, struct instances, etc. are passed by reference to the functions in C#. What happens here is that the original object never gets passed as a parameter to a function. A new reference to the object is passed by value as an argument to the function. So, we end up having two copies of pointers that point at the same memory location. If we modify the value of that pointer, the value of the original object also gets modified. The following code example shows us how to pass an object by reference to a function in C#.

using System;

namespace pass_object_by_reference
{
    public class Sample
    {
        public string s { get; set; }
    }
    class Program
    {
        static void dosomething(Sample obj)
        {
            obj.s = "something";
        }
        static void Main(string[] args)
        {
            Sample sam = new Sample();
            sam.s = "Nothing";
            dosomething(sam);
            Console.WriteLine(sam.s);
        }
    }
}

Output:

something

In the above code, we passed the object sam by reference to the dosomething() function that modifies the s property inside the sam object. It changes the original sam object’s value and assigns something to the sam.s property. Although this is the default way of passing objects by reference, there is one exception to this approach. If we initialize the passed object with the new keyword and the class constructor, a separate copy of the object is created inside the memory. Any changes to the value of that object do not change the original object passed as the parameter. The following code example demonstrates a scenario where this object passed by value to a function in C#.

using System;

namespace pass_object_by_reference
{
    public class Sample
    {
        public string s { get; set; }
    }
    class Program
    {
        static void dosomething(Sample obj)
        {
            Console.WriteLine("Initial passed value = {0}", obj.s);
            obj = new Sample();
            obj.s = "something";
            Console.WriteLine("New value = {0}",obj.s);
        }
        static void Main(string[] args)
        {
            Sample sam = new Sample();
            sam.s = "Nothing";
            dosomething(sam);
            Console.WriteLine("Original Value = {0}",sam.s);
        }
    }
}

Output:

Initial passed value = Nothing
New value = something
Original Value = Nothing

We passed the object sam by value to the dosomething() function in C# in the above code. The dosomething() function takes the argument, prints its initial value, modifies the value, and prints the modified value. This new value is assigned to a new memory location and cannot be accessed by the original object sam.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Class

  • Difference Between Struct and Class in C#
  • Call Constructor From Another Constructor in C#