Quantcast
Channel: Rob Garrett - Blog » Code
Viewing all articles
Browse latest Browse all 10

Pass by Reference

$
0
0

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 &).

Viewing all articles
Browse latest Browse all 10

Trending Articles