Reputation: 44295
In his book, Jon Skeet refers to 7 restrictions on implicit typing. I need clarification on the last two:
A. The type you want the variable to have is the compile-time type of the initialization expression.
B. The initialization expression doesn't involve the variable being declared.
The book covers material in the same order it was released (C# 2 before C# 3). At this point C# 4 has not been introduced so I make the assumption that A does not refer to dynamic
. So, when would the compile-time type be different from the execution time type of the initialization expression?
As for B, when can an initialization expression involve the variable being declared?
Upvotes: 8
Views: 215
Reputation: 841
Regarding B, Henk gave a perfect answer (edit: it's now removed), although I find it peculiar that int x = x = 1;
compiles. (I would've thought x isn't considered declared until after the initializer. Oh, well.) His answer was:
int x = x = 1; // Compiles
var y = y = 2; // Does not compile
Regarding A and your question as to when the compile time type wouldn't match the execution time type, here's an example where they would differ:
var foo = fooFactory.GetFoo();
... and that method on fooFactory is implemented as ....
public FooBase GetFoo() {
return new FooSubtype();
}
Here, foo's type is FooBase (which may be an interface, abstract class, or unsealed concrete class), and (without casting) only its features are available. Clearly, FooSubtype implements or inherits from FooBase.
The type that foo holds at runtime can be discerned here only because I show the implementation of GetFoo(), but it isn't inspected by the compiler. In fact, the implementation may not even be available (could be in another assembly) or it may vary (could be virtual). For determining the compile-time type of GetFoo(), and therefore of foo, only the method declaration is relevant.
Upvotes: 3
Reputation: 1877
My thoughts for A:
It's not that the compile-time is different from the execution type, since even if the execution type is not the same as the compile type (like in any method whose return type is an abstract type), you cannot declare the variable with the execution type anyway with explicit typing.
But you could want to declare the variable with a more abstract static type, even if the real dynamic type can be defined at compile-time. Consider for example:
ISomething a = new MyOwnSomething();
Why would you want to do this? If your MyNewSomething
implements ISomething
explicitly then you would have to make a cast to use it like an ISomething
if declared on a var. Here the cast is still been done, but you don't see the rather ugly:
var a = new MyOwnSomething();
((ISomething)a).Whatever();
A more contrived example is that the initialization code can change later on but and you want to make sure that from this point on you only use a
as an ISomething
, and never see the details of the MyOwnSomething
type, or other interfaces it may be implementing, so that changes on the initialization type won't break the code.
Upvotes: 2