Lecture overview -- Keyboard shortcut: 'u'  Previous page: Polymorphism. Static and dynamic types -- Keyboard shortcut: 'p'  Next page: Type test and type conversion in C# -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Alphabetic index  Help page about these notes  Course home    Specialization, Extension, and Inheritance - slide 32 : 40

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 type    Dynamic 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'.
  }
}
static-dynamic-types-corrected.cs
Corrections of the errors in the illustration of static and dynamic types.