Object-oriented Programming in C# Specialization, Extension, and Inheritance
Static and dynamic types in C#
Knowledge about the static types of variables and expressions is used for compile-time type checking
class A {}
class B: A{}
class Client{
public static void Main (){
// Static typeDynamic type
A x; // A -
B y; // B -
x = new A(); // A A TRIVIAL
y = new B(); // B B TRIVIAL
x = y; // A B OK - TYPICAL
y = new A(); // B A Compile time ERROR
// Cannot implicitly convert type 'A' to 'B'.
y = x; // B B Compile time ERROR!
// Cannot implicitly convert type 'A' to 'B'.
}
}
Illustration of static and dynamic types.
Corrections of the errors in the illustration of static and dynamic types.