8.6.12

Confusion when Passing Reference Type Objects in C#



Everybody knows that when making a method call in C#, reference type objects are passed by reference. In other words, whatever changes you make to your object in the called method will be reflected on the original one.

Let's examine the following code:

    class MyClass
    {
        public int MyInteger;
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.MyInteger = 5;

            Console.Write("Value before method call: ");
            Console.WriteLine(myClass.MyInteger);
            MyMethod(myClass);
            Console.Write("Value after method call: ");
            Console.WriteLine(myClass.MyInteger);
        }            

        private static void MyMethod(MyClass myClass)
        {
            myClass.MyInteger = 10;
        }
    }
If you run the preceding code, it will show the following result:

Value before method call: 5
Value after method call: 10

This was expected. However, the idea that all reference type objects are passed by reference is not really accurate and the following proves so. If we change "MyMethod" by adding one line so it becomes as the following:
        private static void MyMethod(MyClass myClass)
        {
            myClass = new MyClass();
            myClass.MyInteger = 10;
        }

and then we run the program, guess what the result will be. The result will be as following:

Value before method call: 5
Value after method call: 5

The value of the field "MyInteger" did not change as you might expected. That is because what actually happens when you pass a reference type object to a method is that the reference to that object is copied and the copy is passed to the method. The new copy of the reference points to the same object, so if you make changes in the called method, you will be making changes on the same object. But if you create a new object in the called method, you will be actually changing the reference value that was passed and making it point to a new object. And whatever changes you make after this step will be irrelevant to the original object.

The following diagrams illustrates what happens in the previous 2 examples.

Example 1: What really happens when you pass an object
Example 1: What really happens when you pass an object

Example 2: Creating an instance inside the called method
Example 2: Creating an instance inside the called method



If you wish to make any changes to the object in the called method and have them all reflected on the original object even if you make a new instance in the called method, then what you need is to pass it by reference. You need to add the keyword ref before the object type in the method signature and also when calling the method. The code will look like this:

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.MyInteger = 5;

            Console.Write("Value before method call: ");
            Console.WriteLine(myClass.MyInteger);
            MyMethod(ref myClass);
            Console.Write("Value after method call: ");
            Console.WriteLine(myClass.MyInteger);
        }            

        private static void MyMethod(ref MyClass myClass)
        {
            myClass = new MyClass();
            myClass.MyInteger = 10;
        }
    }





4 comments:

Anonymous said...

Please provide diagrammatic representation for ref scenario as well. It will be more clearer.

Alexis Texes said...

You are faith on mordern software ,please contact :-The Validation Software solution article is clearly stating the new software validation process that can work in the favor and help in making the run for any Validation Change Management money. There are different tools and software’s which are enhanced and come with Validation Software abilities making it very true in the present market. For instance the new condition of all works which is happening in the software industry is Electronic Validation quite similar.

David Villa said...

Give your software a best support contact :- In attendance Validation Change Management oscillations near-term up by means of time the developments beached on Validation Software solution in the colonnade. It agreements the entire instrument of us to disruption efficient as healthy as get the premium repetition of internet. The software’s be located overall Electronic Validation in moving in the precise orders. Develop the premium websites as apiece the indispensable as well as Validation Software help of customers.

Justin Abott said...

Give your software a best touc of creative design ,please contact :- In attendance Validation Change Management oscillations near-term up by means of time the developments beached on Validation Software solution in the colonnade. It agreements the entire instrument of us to disruption efficient as healthy as get the premium repetition of internet. The software’s be located overall Electronic Validation in moving in the precise orders. Develop the premium websites as apiece the indispensable as well as Validation Software help of customers.

Post a Comment

Popular Posts