Is there much difference between these 2 if the manipulation is identical? In example 1, a reference to an object is passed, and object is manipulated. In example 2, a reference to an object is passed, the object is manipulated, and then… The same reference is returned?
static void Foo(SomeReferenceType t)
{
//Do something with t
}
static SomeReferenceType Foo(SomeReferenceType t)
{
//Do same thing with t
return t;
}
Bar bar = new Bar();
//Does this
Foo(bar);
//Do the same thing as this
bar = Foo(bar);
The latter allows for chaining methods in a fluent type interface, but it can hide the fact that the object is being manipulated. It’s not very common to manipulate an object that’s being passed that way because it can confuse consumers of the method when their objects come back mutated. What you might do is clone the object, manipulate it, and then return the manipulated clone. This leaves the object in the initial reference intact.
In the former, the consumer is passing a variable and saying, “Dear method, please take this variable and do whatever you want with it including potentially pointing it at a new memory address.” Thus, the developer knows that the state passed in may be manipulated.
By the way, I think you should read this: http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx
When I interview potential developers, I ask a lot of questions about passing reference and value types by reference and by value. I’ve interviewed developers for very senior level positions who don’t understand this but I think it’s fundamental to C# development.