Pass by Reference

Contrary to the beliefs of some developers, .Net reference types do not need to be passed as reference parameters (ref in C#, ByRef in VB) when a method is going to alter the contents of an object instance.

Passing a object reference by reference parameter just means the method can reassign the reference to a new object instance. The following reference parameter shown below is unnecessary:

class foo
{
public int i;
}

… void fooMethod(ref foo fooInst)
{
fooInst.i = 10;
}

.Net is smart enough to pass object instances using address – not the whole object across the stack. In C++ land, the parameter should be passed as a pointer or explicit reference (using &).

2 thoughts on “Pass by Reference

  1. robgarrett

    Right of course, the surprising thing is the number of developers who do not know the difference between value types and reference types. Ask the same group of developers about boxing and you’ll get a blank gaze.

Comments are closed.