Tuesday, 25 May 2010

No Implicit Conversion When Passing Parameters By Reference

We all know that when passing a parameter to a method by reference using the ref keyword, two things are important:

1). the variable must be explicitly assigned first.
2). the variable doesn't create a new storage location (in terms of reference type) or new copy (in terms of value type).

However, one undocumented (by undocumented I mean it is neither in C# specification nor MSDN) feature of passing parameter by ref is the declaration type of the variable passed in must match exactly the declaration type of the parameter in the method. For example, if you have a method like this:

  public void RefMe(ref ValueType pop)
{

}

then the following code will give you a compile error:

int number = 3;
RefMe(ref number);

Even though integer is subtype of ValueType, but implicit conversion/casting is not possible when passing parameters reference. The variable declaration type must match the method parameter type exactly.

Don't attempt the following either :

RefMe(ref (ValueType)number); It will emit a compile error.

You have to do the casting before the method call:

int number = 3;
ValueType val = number;
RefMe(ref val);
or
ValueType number = 3;
RefMe(ref number);

but be aware that for value types, declaring another variable creates a new copy of the data, which may defeat the purpose of passing a variable by reference. However, it is fine for reference type though.




















No comments:

Post a Comment