Wednesday, October 7, 2009

Covariance and the "ref" Keyword.

Here's a slight little detail worth noting.

If you're passing a value into a method that uses the "ref" keyword, the type of the variable passed must match the ref parameter exactly.

The value passed into the ref method is reassigned upon the method's completion, so the object reassigned must match the variable's declared type. If a variable's type is only derived from the parameter type requested by the method, there would be nothing stopping the method from reassigning the ref parameter internally thus causing a type mismatch on the method's completion.