Detail 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.
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());
}
}A
B
C
A summary. We demonstrated the GetType method when used with derived and base classes. This C# method returns the most-derived class—it does not return base classes.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 24, 2022 (image).