Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;

we pass int i into a method like: method(i) Passing by reference means exactly passing its location like :

    Class.method.variable

and it will give out the value. Can anybody help me out??

like image 483
Jay Singh Avatar asked Dec 19 '22 22:12

Jay Singh


1 Answers

In simple terms...

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9.

"Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

This has various consequences, and each is useful in different situations.

This answer has more thorough information: What's the difference between passing by reference vs. passing by value?

like image 172
Jacob Avatar answered Dec 22 '22 11:12

Jacob