C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Base classes are described with transitive closure. This affects the types allowed as variables. GetType returns a type pointer.
Info: The class A is used as base class. The class B derives from class A. And the class C derives from class B.
Main: This executes and creates an instance of each class, but stores the references in the A type variables.
Next: The GetType method returns the most derived type of the instances, not the type of the variable.
C# program that uses GetType on base type instance
using System;
class A
{
}
class B : A
{
}
class C : B
{
}
class Program
{
static void Main()
{
A a1 = new A();
A a2 = new B();
A a3 = new C();
Console.WriteLine(a1.GetType());
Console.WriteLine(a2.GetType());
Console.WriteLine(a3.GetType());
}
}
Output
A
B
C
And: This example shows how transitive closure applies when you assign a base class reference to a class derived over two levels (A to C).
Review: GetType accesses the actual type of the object instance. It does not report the base types or the variable type.
Reflection