Are arrays or lists passed by default a reference in C #?

They? Or speed up my program if I pass them by reference?

+59
arrays reference c #
Jun 08 '09 at 22:45
source share
4 answers

the link is passed by value.

Arrays in .NET are an object on the heap, so you have a link. This link is passed by value, which means that changes to the contents of the array will be visible to the caller, but the reassignment will not:

void Foo(int[] data) { data[0] = 1; // caller sees this } void Bar(int[] data) { data = new int[20]; // but not this } 

If you add the ref modifier, the link passed by reference - and the caller can see either the change above.

+160
Jun 08 '09 at 22:47
source share

They are passed by value (like all parameters that are neither ref nor out), but this value is a reference to the object, so they are effectively passed by reference.

+7
Jun 08 '09 at 22:47
source share

Yes, they are passed by default link in C #. All objects in C # except value types. To be more precise, they are passed "by reference by value"; that is, the value of the variable that you see in your methods is a reference to the original passed object. This is a small semantic point, but sometimes it can be important.

+2
Jun 08 '09 at 22:46
source share

An array is a reference. And it is transmitted by value.

0
Jun 09 '09 at 3:17
source share



All Articles