Home
C#
GetType Method
Updated May 24, 2022
Dot Net Perls
GetType. This is a C# method on object. It provides a Type object. This object indicates the most derived type of the object instance.
Type
object
Returned value. GetType in .NET accesses the actual type of the object instance. It does not report the base types or the variable type.
Example program. This program uses the GetType method on a base type reference to more derived objects. We instantiate 3 classes—A, B and C—in Main.
class
new
Constructor
Info The class A is used as base class. The class B derives from class A. And the class C derives from class B.
base
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.
Reflection
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 24, 2022 (image).
Home
Changes
© 2007-2025 Sam Allen