This is a very very old topic and there is lot of information about it, so I’m not going to enter in detail about it.
Before to start, I’m just going to say that value types contain their data directly and reference types contain a reference to their data stored in a different place of the memory. In the case of .NET we know that value types are stored in the stack and reference types in the heap.
I think nowadays everybody has this concept clear, the whole idea behind this post comes because today I was reading some code and I so a “small” bug, probably due to a misunderstanding of what implies to pass reference types as parameters. Take a look to the next code.
What do you think will be the output? Somebody could answer 0,1,2,3 but the output is 0,1,2,2.
But why is this? The answer is quite easy, what we do in methods DoTest1 and DoTest3 is to pass a reference type by value; in the method DoTest2 we pass reference type by reference.
This means that methods DoTest1 and DoTest2, receive a reference to the value and consequently we can modify the value. But in method DoTest3 we are trying to modify the reference which is not possible because we passed it by value. So, to be able to modify the reference we need to pass it by reference and this is done adding the “ref” operator.
After we do this change in method DoTest3 the output will be 0,1,2,3