1

You have these classes shown below:

public class A
{
}

public class B : A
{
}

You cast the base class to a type of the derived class

A w = (B) new A();    
B x = (B) new A();

This will not work on the run time because you cannot really convert a base class to a derived class.

But why is there no compile time error? why does visual studio allowed me to reach run-time before throwing the error?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
raberana
  • 11,739
  • 18
  • 69
  • 95
  • 1
    This may help: http://ericlippert.com/2012/07/10/696/ – Blorgbeard Apr 26 '15 at 04:13
  • C# compiler allows `explicit` casting from derived class to base class, but you have to ensure there is no runtime error – Khanh TO Apr 26 '15 at 04:16
  • This is actually wrong, whether you write an explicit or implicit cast, at runtime you'' receive an exception as the conversion to a derived class is not allowed. – Aydin Apr 26 '15 at 04:25

2 Answers2

3

There are 2 types of casts

  • once that clearly not allowed when classes have no common base and hence cast have no chance to succeed. I.e. 'string' to 'int'. Such casts are caught by compiler and cause errors.
  • casts that have chance to succeed - base to derived have reasonable chance to succeed. Compiler allows such casts.

I believe the reason why (B)new A() is allowed at compile time even if cast is guaranteed to fail is because (B)someObjectOfTypeA can succeed and new A() is definitely one of such "object of type A". Compile time detection likely would require additional infrastructure and was not found beneficial (as this cast immediately fails at runtime hence have low chance to be missed by even most basic testing of your code).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • ahhh.. so what you are implying is that microsoft could have a way to raise an error right on the compile time but found it to be a minor issue because they are sure it will fail run time? – raberana Apr 26 '15 at 04:21
  • "base to derived have reasonable chance to succeed" - can you give an example of this? – raberana Apr 26 '15 at 04:22
  • "could have a way to raise an error right on the compile time " - this is my guess. I'm not familiar with specification enough to know if it is actually requirement or just happens to be that way. – Alexei Levenkov Apr 26 '15 at 04:24
  • 1
    @RojBeraña `(B)(A)(new B())` - 100% chance to succeed to cast variable that has compile type A - `(A)new B()` - to derived class B. There are also plenty of casts that should succeed but compiler can't figure it out when you deal with generics and you see strange code like `(T)(object)argument` (some interesting cases covered in Eric Lippert's [article](http://ericlippert.com/2012/07/10/696/) linked earlier in comments) – Alexei Levenkov Apr 26 '15 at 04:25
  • real world scenario where it could succeed: Animal a = new Dog(); Dog d = (Dog) a; – raberana Apr 26 '15 at 05:19
1

In c# casting is in runtime for user defined classes, that is why compiler doesn't throw error. You can see this for more info. The casting from one basic type (string) to another basic type (int) is known as compile time, as compiler knows string can't be type casted to int! But in user defined classes, there is a chance! :)

Community
  • 1
  • 1
Abhishek
  • 6,912
  • 14
  • 59
  • 85
  • but why does an error is thrown already in compile time with this code: int d = 23; var f = (bool) d; – raberana Apr 26 '15 at 04:07
  • huh.. Missed this point. int, and bool are known types and there is no chance that in runtime they will get casted correctly, hence you see the error. Edited answer accordingly :) Sorry, my mistake. – Abhishek Apr 26 '15 at 04:52