C# reference types are passed by value!


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.

57 thoughts on “C# reference types are passed by value!

  1. 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.

  2. 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.

  3. 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.

  4. 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.

    http://msdn.microsoft.com/en-us/library/s6938f28.aspx

  5. 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.

  6. on having one of the most superior blogs I’ve stumbled upon in a while! Its just hard to believethe amount you can take away from something purely because of how visually beautiful it is. You’ve set together a fantastic blog space –magnificent visuals, videos, layout. This is definitely a must-see website!

  7. I really enjoyed studying your blog, you have some good points. It made me want to bookmark your article. I savedyou to Hi 5 and google favorites. I will make time to return to your article and examine more articles.

  8. After reading this I thought it was very informative. I appreciate you taking the time and effort to put this post together. Once again I find myself spending way to much time both reading and commenting. But so what, it was still worth it!

  9. After reading this I thought it was very informative. I appreciate you taking the time and effort to put this post together. Once again I find myself spending way to much time both reading and commenting. But so what, it was still worth it!

  10. For now, a lot of tips abbreviated and adapted to start in this field. Encourages me to advance cross on your page by accident. I’ll be subscribing to their increased power to get the latest updates. Appreciate all the tips here

  11. Just want to say your article is astounding. The clarity in your post is simply spectacular and i can assume you are an expert on this field. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the fabulous work.

  12. Just want to say your article is astounding. The clarity in your post is simply spectacular and i can assume you are an expert on this field. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the fabulous work.

  13. Website made here. I agree to do an analysis on the subject and start a huge amount of human beings to adhere to your blog. Certainly, these practices are unfair, but they say much of its rules alone administer to humans by overdraft.

  14. Analytical’ve been affiliated with this admission. He did not leave in abundance, but so detailly. We put plenty of fans. I cheer for you, you can emerge from the warning here. We abound together.On added hand, warn that some web sites is well acceptable.

  15. I appreciate reading what you had to disclose, You have an astounding understandingon the subjectinformationand I look forward to studying more of what you have to say. I will pay attention and bookmark your website and go back to your blog when an update is announced.

  16. I want to say that I haven’t glanced at something so motivating in a long time. There are many motivating thought and concepts. I suppose that you without doubt discovered an vital detail and I signed up to your rss feed to stay informed.

  17. Its really a great post. I am sure that anyone would like to visit it again and again. After reading this post I got some very unique information which are really very helpful for anyone. This is a post having some crucial information. I wish that in future such posting should go on.

  18. Can I make a suggestion? I believe youve had sth good here. However let’s say you provided a few references towards a website that backs up what you were saying? Or even you may provide us with some extra information to look at, something might link up what exactly you’re mentioning, sth more concrete?

  19. Howdy, i read your blog occasionally and i own a similar one and i had been just wondering should you get lots of spam comments? If so how do you avoid it, any plugin or anything you are able to advise? I get so much recently it’s driving me mad so any assistance is very much appreciated.

Comments are closed.