Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile-time and runtime casting c#

I was wondering why some casts in C# are checked at compile-time whereas in other cases the responsibility is dumped on CLR. Like above both are incorrect but handled in a different way.

class Base { }
class Derived : Base { }
class Other { }

static void Main(string[] args)
{
    Derived d = (Derived)new Base();     //Runtime       InvalidCastException
    Derived d = (Derived)new Other();    //Compile-time  Cannot convert type...
}

While reading "C# in depth" I've found the information on this topic where autor says:
"If the compiler spots that it’s actually impossible for that cast to work, it’ll trigger a compilation error—and if it’s theoretically allowed but actually incorrect at execution time, the CLR will throw an exception."

Does 'theoretically' mean connected by inheritance hierarchy (some another affinity between objects ?) or it is compiler's internal business?

like image 633
nan Avatar asked Dec 21 '10 13:12

nan


People also ask

What is compile-time and runtime in C?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is difference between compile-time and runtime error?

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error.

What is compile-time error in C?

Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by the compiler and thus are known as compile-time errors.

Is ClassCastException a compiler error?

If the program were compiled and the program run, every evaluation of this expression would throw a ClassCastException. There- fore, this is expression is deemed a compile-time error, a syntax error, and the expression will not be compiled.


2 Answers

  • Upcasts can be checked at compile time - the type system guarantees that the cast succeeds.
  • Downcasts cannot (in general) be checked at compile time, so they are always checked at runtime.
  • Unrelated types cannot be cast to each other.

The compiler considers only the static types. The runtime checks the dynamic (runtime) type. Looking at your examples:

Other x = new Other();
Derived d = (Derived)x; 

The static type of x is Other. This is unrelated to Derived so the cast fails at compile time.

Base x = new Base();
Derived d = (Derived)x; 

The static type of x is now Base. Something of type Base might have dynamic type Derived, so this is a downcast. In general the compiler can't know from the static type of x if it the runtime type is Base, Derived, of some other subclass of Base. So the decision of whether the cast is allowed is left to the runtime.

like image 184
Mark Byers Avatar answered Nov 03 '22 00:11

Mark Byers


If your variable is of Base type, is can be theoretically constructed by Derived constructor, thus being a variable of Derived type actually. At compile time, compiler does not bother itself with trying to figure out whether in each particular case such downcast (representing a variable of Base type as an entity of Derived type) is possible.

Your sample is simple - you create a new class and cast it right away. But what if you get Base from somewhere else, e.g., some method call? Compiler just cannot "guess" what your method is going to return and therefore throw on not throw an error.

When you cast Other, compiler sees that there is no possibility that Other is actually Derived and throws an exception.

like image 39
Michael Sagalovich Avatar answered Nov 03 '22 01:11

Michael Sagalovich