Passing Objects By Reference or Value in C#

Lots of good answers had been added. I still want to contribute, might be it will clarify slightly more.


When you pass an instance as an argument to the method it passes the copy of the instance. Now, if the instance you pass is a value type(resides in the stack) you pass the copy of that value, so if you modify it, it won’t be reflected in the caller.

If the instance is a reference type you pass the copy of the reference(again resides in the stack) to the object. So you got two references to the same object. Both of them can modify the object.

But if within the method body, you instantiate new object your copy of the reference will no longer refer to the original object, it will refer to the new object you just created.

So you will end up having 2 references and 2 objects.

Similar Posts