Parameter passing in C#

I just read this article by MVP Jon Skeet on Parameter passing in C#. Definitely good reading. It helps to understand exactly how reference and value types work when passing them through methods. Here is a good example of how this helped me:
– Instantiate an instance of Class1 as obj1.
– Instantiate Class2 as obj2.
– Pass obj1 in obj2.Foo(obj1)
– The obj2 also has an instance var for Class1 called myobj1.
– In obj2.Foo(Class1 yourobj1), assign myobj1 = yourobj1.

My question has been if myobj1 is a clone of the value of obj1, or if it’s a real reference to the same value. Indeed both references point to the same space, so I need to be careful about how the instance is handled as I pass it around an application. In VB the word SET would have made this all obvious. The article also made it very clear how passing a reference type with a ref qualifier worked – something that can be very confusing.

See Jon’s other articles and his blog for other good info.


Leave a Reply