PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How parameter by reference/value works in C#

 July 06, 2022     c#, parameter-passing, pass-by-reference, pass-by-value     No comments   

Issue

I have this sample code:

    public class MyClass
    {
        public int Value { get; set; }
    }


    class Program
    {
        public static void Foo(MyClass v)
        {
            v.Value = 2;
            v = new MyClass();
            v.Value = 3;
        }

        static void Main(string[] args)
        {
            var m = new MyClass();
            m.Value = 1;
            Foo(m);
            Console.Write(m.Value);
            Console.ReadLine();
        }
    }

I would like to understand why the output is 2 and not 3, could you please give me some clear explanation?

Thanks


Solution

I will go through with you, step by step via the debugger and we will see what it is 2.

Step1

We see that we entered into Foo and we passed an instance of MyClass by reference v (class instances in C# are passed by reference by default)

In memory, we would see something like this:

v = 0x01; //0x01 - is a simple representation of a pointer that we passed
v.Value = 1;

next, we step over and we see that we change the value Value in our reference. step 2

v = 0x01;
v.Value = 2; // our new value

and then we assign new to our v so in memory we have

v* = 0x01 // this is our "old" object
v*.Value = 2;
v = 0x02 // this is our "new" object
v.Value = 3;

as you can see we have 2 objects in the memory! The new one v and the old one marked with a start v*

when we exit the method, we didn't replace the content of the memory address 0x01 but created a local copy of v for the scope of the functions, and we created a new object under the memory address 0x02 which is not referenced in our Main method.

step3

Our main method is using instance from address 0x01 not new 0x02 we created in Foo method!

To make sure we pass the right object out, we need to tell C# that we want to either "edit" output using ref or we want to "overwrite" output using out.

Under the hood, they are implemented the same way!

Instead of passing 0x01 to our Foo method, we pass 0x03! which has a pointer to our class under 0x01. So when we assign v = new MyClass() when we use ref or out, we, in reality, modify the value of 0x03 which is then extracted and "replaced" in our Main method to contain the proper value!



Answered By - Tomasz Juszczak
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing