Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# constructor, object parameter is passed by reference or value

Tags:

c#

If you have class and a constructor which takes in an object as a input param - is that object passed by reference or is it passed by value?

And is it true to assume that for class methods, object input parameters are passed by value by default unless the ref keyword is used?

What about the out keyword? Does this still mean that it is passed by reference?

like image 397
Bobbo Avatar asked Jan 06 '11 16:01

Bobbo


4 Answers

If you have class and a constructor which takes in an object as a input param - is that object passed by reference or is it passed by value?

All parameters are passed by value in C# unless the parameter is marked with out or ref.

This is a huge source of confusion. I'll state things a little more explicitly.

All parameters have their value copied unless the parameter is marked with out or ref. For value types, this means that a copy of the value being passed is made. For reference types this means that a copy of the reference is made. For this last point, the value of a reference type is the reference.

And is it true to assume that for class methods, object input parameters are passed by value by default unless the ref keyword is used?

Again, all parameters are passed by value in C# unless the parameter is marked with out or ref. For a parameter marked with ref, a reference to the parameter is passed to the method and now you can think of that parameter as an alias. Thus, when you say

void M(ref int m) { m = 10; }

int n = 123;
M(ref n);

you can think of m in M as an alias for n. That is m and n are just two different names for the same storage location.

This is very different from

string s = "Hello, world!";
string t = s;

In this case, s and t are not alises for the same storage location. These are two different variables that happen to refer to the same object.

What about the `out keyword? Does this still mean that it is passed by reference?

The only difference between ref and out is that ref requires the variable to be initialized before being passed.

like image 84
jason Avatar answered Oct 08 '22 18:10

jason


The reference to the object will be passed by value.

.NET has reference types and value types - classes are all reference types and structs are value types. You can pass either one by value or by reference.

By default, everything is passed by value, the difference being that with reference types the reference is passed in.

The ref and out keywords will cause the parameters to be passed by reference - in the case of value types that means you can now make changes that will be reflected in the passed in object. With reference types that means you can now change the object that the reference refers to.

like image 22
Oded Avatar answered Oct 08 '22 17:10

Oded


An object is always passed by reference to the actual object. So no copy (aka "by value") is being performed of the object.

Just, as Oded notes, the reference to the object is being copied.

like image 23
Uwe Keim Avatar answered Oct 08 '22 16:10

Uwe Keim


The default passing mechanism for parameters in .Net is by value. This is true for both reference and value types. In the reference case though it's the actual reference which is passed by value, not the object.

When the ref or out keyword is used then the value is indeed passed by reference (once again true for both value and reference types). At a CLR level there is actually no difference between ref and out. The out keyword is a C# notion which is expressed by marking a ref param (I believe it's done with a modopt)

like image 37
JaredPar Avatar answered Oct 08 '22 17:10

JaredPar