I have been reading up on Reference and value types. I get the idea. But how does this help me while coding? I don't have any good examples to understand those times where knowing this stuff helps.
-
I can't even really think of a reason or scenario in which you could use a ref param. Anyone have some good real-world examples of when they actually used this? I've never seen it in any code in any team I've been in even .coms. I refuse to use sloppy out params. – PositiveGuy Jan 19 '11 at 05:48
4 Answers
It means you can understand what your code is going to do.
It's kinda hard to write code when you can't predict what the effects of any given statement will be, due to not knowing the type system semantics.
For example, suppose you don't know how reference types work, and someone presented you with this code:
StringBuilder builder = new StringBuilder();
StringBuilder other = builder;
builder.Append("Foo");
other.Append("Bar");
Console.WriteLine(builder);
We know that that will print "FooBar" because StringBuilder
is a reference type - the second line is only copying a reference, not creating another object. But if you didn't understand how reference types behave, you might have expected it just to print "Foo".

- 1,421,763
- 867
- 9,128
- 9,194
-
1
-
He could of used "because Jon Skeet told you to" and it would have been a perfectly accepted answer too ;) – Matthew Abbott Dec 03 '10 at 16:06
-
When you say copy a reference, isn't other just pointing to the memory address of the builder object? Is that what you're saying? – PositiveGuy Dec 04 '10 at 04:20
-
1@CoffeeAddict: The exact form of the reference is an implementation detail - it doesn't *have* to be a pointer to a memory address, although that's the most likely implementation. See http://blogs.msdn.com/b/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx – Jon Skeet Dec 04 '10 at 07:24
-
@Jon, then I don't get it. When can a reference not be a pointer. Never figured this. – PositiveGuy Dec 13 '10 at 18:58
-
1@CoffeeAddict: See http://wikis.sun.com/display/HotSpotInternals/CompressedOops for a Java example - a way of representing a reference such that it isn't just a bare pointer. – Jon Skeet Dec 13 '10 at 19:23
-
1Sorry, had to be quick there. Basically, a pointer is a very specific representation - whereas a reference is *any* way at getting at an object. Imagine a distributed VM: a "reference" could include the name of the machine which currently "hosts" the object. – Jon Skeet Dec 13 '10 at 19:31
Value types and reference types differ in how they work, how they are optimised, where they are stored etc. Although you can probably get away with writing applications without any real knowledge of how they differ, it is so much more beneficial to understand and appreciate why you use certain types for certain things.
For instance:
- Why can't I assign a
null
to myint
? - Why when I pass in an integer into a function does it not update my local variable?
- Where are variables stored in memory?
It's a fundamental part of the design of the runtime. Let's not forget that it comes straight out of the Interviewer's question handbook!

- 60,571
- 9
- 104
- 129
-
not sure what you mean by local variable here: "Why when I pass in an integer into a function does it not update my local variable?" – PositiveGuy Dec 04 '10 at 04:21
-
The only bullet point listed above that is related to what I am getting at is your int and why it cannot be set to null. That's a good example. But the others are just "definition". Anyone can state this stuff and spit it out in an interview. I do not want to only spit it out, I want to understand how it applies when I actually code other than the stupid examples Microsoft shows which give you no idea in the real world how this applies. – PositiveGuy Dec 04 '10 at 04:23
For example:
void ChangeDate(DateTime dt)
{
dt.Year = 2011;
}
DateTime dt = new DateTime(2010,1,1);
ChangeDate(dt);
Console.WriteLine(dt);
Since DateTime is a struct
(value type), it is passed by value (if not the in
,out
or ref
specifiers are present). The console output here still prints 2010.
What is going on? Since DateTime
is passed by value, a copy of that object is passed into the method. If the method modifies the object, only the copy is modified.
So what is new
good for? reference objects are created on the heap and must be allocated first. This is different for value types. The new
operator for value types just copies an initialized object over your old object.

- 28,510
- 21
- 92
- 151
-
Got it, good example and good point on your first line about if no in, out, or ref specifiers are present. Since a copy of the dt instance is sent into ChangeDate, it just changes it but there's no refrence back to the original object (because it's not a ref type) so calling the method has no effect on the original creation so your dt variable is still pointed to that initial value type instance thus it still gonna print 2010. – PositiveGuy Dec 04 '10 at 04:30
Suppose you see this code:
static void changeVal (Foo i){
i.Int = 5;
}
public static void RunSnippet()
{
Foo foo = new Foo(){Int = 0};
changeVal(foo);
Console.WriteLine(foo.Int);
}
Can you predict output? No, because if Foo is class then you will get 5 and if it is struct you'll get 0.

- 18,267
- 14
- 46
- 55
-
-
I know a struct is a value type. Ok but I don't get then how you're getting 5 in the case of the ref type vs. 0 with the struct value type. I mean you're not using a ref keyword for the Foo class/object instance..so am I way off base here? – PositiveGuy Dec 04 '10 at 04:25
-
1If Foo is class then it always passes as ref, so ref keyword is not necessary – Denis Palnitsky Dec 06 '10 at 09:14
-
that's weird, then is that a shortcut? why does C# have a ref keyword then? – PositiveGuy Dec 06 '10 at 16:46
-
Here are lots of questions about it http://stackoverflow.com/questions/635915/when-to-use-ref-and-when-it-is-not-necessary-in-c – Denis Palnitsky Dec 07 '10 at 12:28
-
1Do not confuse the 'ref' parameter modifier with reference or value types. This MSDN documentation should provide sufficient explanation: http://msdn.microsoft.com/en-us/library/14akc2c7(v=VS.90).aspx – James Dunne Dec 14 '10 at 01:49