Bruce Wood said:
No, Scott got it right. Using var in the way you described is like a
poor man's covariant return type. Just for the record, I also agree
that it's a risky use of var. Using interfaces is a far better idea:
I certainly wouldn't consider it close to covariant return types, since the
actual type doesn't change in a descending class(which would be extremely
useful and would use natural polymorphism instead of hacks, same goes for
contravariant parameters). The class still has no methods overloaded on
return types. The range of types the call site will accept grows, but the
range of types returned doesn't. Mild difference I know, still.
At any rate, I still don't like that usage. IMHO var should use a pattern
akin to:
var IDENTIFIER = new EXPRESSION;
or
var IDENTIFIER = LINQ_QUERY;
and nothing else(it'd be nice if this was atleast a warning in the
compiler). Using it to accept any type returned by an expression seems
risky, but I see no problem with using it to shorten construction
expressions(prefer it over using aliases actually, which pushes the actual
type to the top of the file) and for anonymous types. A bit ugly, perhaps,
but I think we will all learn to deal with that rather rapidly. Using var
for quick and dirty, "type this as what this method returns" is a dangerous
road, IMHO.