C# reference types are passed by value!

19. August 2010

How many times I read: "In C# reference types are passed by reference, primitive types by value"? The essential point is that reference types has nothing to do with passing by reference!

But let us start with some basics. There are two types of objects, the value types (structs) where the variable is the object and the reference types which only points to the object. When you pass in a variable to a method, its value gets copied to the method by default. For values types, that means the object itself gets copied. For reference types, that means only the thing that points at the object gets copied!

It is a little bit confusing, because only the reference to the object is copied, not the entire structure. It is a way to save performance, otherwise as larger as the reference type would be as more performance it would cost. This strategy is also know as call by sharing (which makes it a less confusing than calling it also call by value with reference types). So in a call by value scenario, if the data of a reference type is changed inside the method, the caller variable is also affected. If you change a the value of a value type, which is passed to a method, it will not affect the caller variable.

public void Sample()
{
Point point = new Point(20, 30);
Person person = new Person() { Name = "Meier" };

CallByValue(point, person);

Console.WriteLine("Point X: {0}", point.X);
Console.WriteLine("Name: {0}", person.Name);
}

public void CallByValue(Point point, Person person)
{
point.X = 10;
person.Name = "Müller";
}

The ref keyword indicates in C# a call by reference. When pass a value type by reference to a method, the changes to the value which are done in the method scope, will also affect the variable in the code of the caller. But what happens if you pass reference type to a method by ref?

public void Sample()
{
Point point = new Point(20, 30);
Person person = new Person() { Name = "Meier" };

CallByValue(ref point, ref person);

Console.WriteLine("Point X: {0}", point.X);
Console.WriteLine("Name: {0}", person.Name);
}

public void CallByValue(ref Point point, ref Person person)
{
point.X = 10;
person = new Person() { Name = "Müller" };
}

It allows to change the instance which the variable points to. So you can assign a new object to the variable and also the caller variable will point to the new object. By the way the behaviour in VB.NET is similar to the behaviour of C#, the only difference is that in VB.NET you always have to define if you want to pass a value by value or reference.

DotnetKicks
DotNetShoutout

,

Comments

8/19/2010 9:23:58 PM #
C# reference types are passed by value!

Thank you for submitting this cool story - Trackback from DotNetShoutout
8/19/2010 9:24:08 PM #
C# reference types are passed by value!

You've been kicked (a good thing) - Trackback from DotNetKicks.com
8/21/2010 7:20:28 PM #
Oh how wrong you are.

Reference types are passed by reference. That's what it says, because that's what it does: you only pass a reference to the object.

It's the object that you pass, not the variable that points to the object. Not unless you specify an 'out' or 'ref' parameter.

Granted, it could have been described better, but it's not because you want to give it a different wording that the whole world is going to follow.
paul
paul
8/23/2010 12:25:37 AM #
Re: "Oh how wrong you are."

No, he is not wrong - just pointing at typical sloppy grammatical usage. The reference type is passed by value. The referring object is conceptually passed by reference.
8/23/2010 8:00:03 AM #
The reference type is not passed at all. It's a type.

A variable of a reference type is passed by value, because it contains a reference, but the object is not passed, it's the variable that is passed. But when talking about 'pass by value' or 'pass by reference' we're not talking about the variable, but about the object. And that is still passed by reference.

So it's basically about what it is that we're passing, when we talk about 'pass-by-value' or 'pass-by-reference', and where Mattia would like to have everyone talking about the variable, the rest of the world talks about the object itself so that the distinction is clear about what happens to the object if the callee changes it.

Ardiel
Ardiel
8/23/2010 2:09:23 PM #
From MSDN:
A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword. For simplicity, the following examples use ref.

msdn.microsoft.com/en-us/library/s6938f28.aspx
8/23/2010 3:20:06 PM #
thanks for posting this link, didn't know that such a article exists on MSDN!
Daniele
Daniele
8/24/2010 3:29:11 PM #
IMHO, the correct terms might be "passing by..."
- "value", for structures (value types), without any ref or out modifier.
- "reference", for structures and classes (objects), with ref (or out) modifier.
- "handle", for classes, without any ref or out modifier.

This is how the framework really works, because any object instance has an handle assigned to it that uniquely identifies it, its location in memory and, of course, its data.
3/15/2011 1:22:01 AM #
Pingback from sixteen.alloywheelsguide.com

Pro Rider package now available from CMRC | The Motocross Blog
Comments are closed