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 and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 32 : 40
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 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'.
  }
}

Illustration of static and dynamic types.

/user/normark/oop-csharp-1/sources/c-sharp/inheritance/static-dynamic-types-corrected.csCorrections of the errors in the illustration of static and dynamic types.