C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# GetType Method: Get The Object Type

In this tutorial, we will explore the GetType method in C#. The GetType method is used to get the Type object that represents the runtime type of an object.

  • Understanding GetType

The GetType method is part of the Object class, which is the base class for all classes in C#. When you create a new class, it inherits the GetType method from Object.

The GetType method returns an instance of the Type class that contains information about the object's runtime type, such as its name, assembly, properties, methods, and so on.

  • Using GetType

Here's an example of how to use the GetType method to get the type of an object:

Person person = new Person { FirstName = "John", LastName = "Doe", Age = 30 };

Type personType = person.GetType();

Console.WriteLine(personType.FullName); // Output: Person (assuming Person is not in any namespace)

In this example, we call the GetType method on a Person object to get its Type object. Then, we print the full name of the Person type.

  • Using the 'is' Operator

In many cases, you don't need to use the GetType method to check if an object is of a certain type. Instead, you can use the is operator:

bool isPerson = person is Person;
Console.WriteLine(isPerson); // Output: True

The is operator returns true if the object is of the specified type or a derived type.

  • Using the 'as' Operator

If you want to check the type of an object and cast it to that type at the same time, you can use the as operator:

Person person2 = new Person { FirstName = "Jane", LastName = "Doe", Age = 28 };
object obj = person2;

Person castedPerson = obj as Person;
if (castedPerson != null)
{
    Console.WriteLine("The object is a Person");
}
else
{
    Console.WriteLine("The object is not a Person");
}

In this example, the as operator tries to cast the obj object to a Person. If the cast succeeds, it returns the casted object; otherwise, it returns null.

This tutorial demonstrates the basics of the GetType method in C#. By using the GetType method, you can obtain information about the runtime type of an object and make decisions based on its type. However, in many cases, you can use the is and as operators instead of the GetType method for simpler type checking and casting.

  1. How to use GetType in C#:

    • Description: GetType is a method in C# used to retrieve the runtime type information of an object.
    • Code:
      object myObject = "C# GetType Example";
      Type objectType = myObject.GetType();
      
  2. Using GetType with polymorphism in C#:

    • Description: GetType works seamlessly with polymorphism, providing the actual runtime type of the object.
    • Code:
      Animal myAnimal = new Cat();
      Type animalType = myAnimal.GetType();