can somebody explain me what does “passing by value” and “Passing by reference” mean in C#?

前端 未结 4 1577
悲哀的现实
悲哀的现实 2021-01-20 00:50

I am not quiet sure about the concept of \"passing by value\" and \"passing by reference\" in c#. I think Passing by value mean :

    int i = 9;
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 01:18

    Few Points:

    In C# unless, you use ref keyword, it is always Pass By Value.

    Pass By Value: In case of Value Type - It is just another copy of Value Type Variable.

    In case of Reference Type - It is a copy of reference (or pointer) to exact location where memory is assigned in heap.

    Pass By Reference:

    for Reference type variable - passing it to another method by using keyword is only useful in the scenario when the passed variable can be reassigned in the called method. It means :

    let's say - Outside (caller) method A inside (called) method B

    Reference Type Object R -passed to method B from method A then if you reassign the memory location of Object R in method B then those changes wouldn't be seen in Method A of Object R unless you pass the object R using ref keyword (i.e pass by reference).

提交回复
热议问题