What exactly happens when new B() and
new A() gets called?
Here line 1 compiles successfully
while line 2 displays typecasting
error. Why this happens.
Since B
subclasses A
, it is valid for a reference of type A
to refer to an object of run-time type B
. After all, B
is simply a "special case" of A
.
However, the converse is not true, because not all A
s can be considered B
s.
Although this is strictly enforced by C#'s safe type-system even if there is no "real" incompatibility, the reasons for such restrictions are natural. Imagine, for example, that B
declared a property public int Foo {get; set;}
.
How would you expect this to behave:
B ob = new A();
ob.Foo = 5;
This is clearly illogical: the real object that the reference is referring to has no such property. Consequently, the compiler prohibits such constructs.
Now imagine you changed your code to:
B b = (B)new A();
Here, you are telling the compiler that the object created, will, at run-time, be assignable to a reference of type B
. This will compile fine, but since the assertion is clearly incorrect, a run-time InvalidCastException
will be thrown.
To summarize, C#'s type system (if you ignore dynamic
and a few special cases) is both static and safe: you will not successfully be able to treat a concrete instance of A
as though it were of type B
.