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.
Wednesday, October 7, 2009
Subscribe to:
Post Comments (Atom)


2 comments:
Just to confirm, this means NO covariance for ref parameters?
It's possible to declare an IFoo, instantiate it as a new Foo(), then pass it in by reference and reassign the value to a Foo2 (which also implements IFoo) in a method that accepts "ref IFoo", but it's not possible to accept "ref IFoo" and pass in a variable declared and instantiated as "Foo".
So yes.
Post a Comment