Type Checking in C#

The purpose of this article is to introduce you to C# type checking.

Using typeof, we can determine the type of built-in types such as int, float, etc, and user-defined types such as classes, interfaces, delegates, and structs.

int num = 1;
Employee emp = new Employee();
Console.WriteLine(typeof(Employee));
Console.WriteLine(typeof(int));

There is no way to pass user-defined type objects and built-in type variables through typeof.

int num = 1;
Employee emp = new Employee();
Console.WriteLine(typeof(emp)); // throws compile time error
Console.WriteLine(typeof(num)); // throws compile time error

The typeof function checks the type at compile time. User-defined type objects may be found and returned with the GetType() method.

int num = 1;
Employee emp = new Employee();
Console.WriteLine(emp.GetType());
Console.WriteLine(num.GetType());

Built-in types and user-defined types do not have the GetType() method.

int num = 1;
Employee emp = new Employee();
Console.WriteLine(Employee.GetType()); // throws compile time error
Console.WriteLine(int.GetType()); // throws compile time error

GetType() methods take two types of names, static and instance. Static GetType() methods take names that are fully qualified, for example, Namespace.  TypeName must include the namespace or else it will throw an exception.

Type type = System.Type.GetType("System.Int64");
Console.WriteLine(type.Name);
Employee emp = new Employee();
Console.WriteLine(emp.GetType());

GetType() Method checks type at runtime.

Below is an example of using typeof and GetType() Method in C#

class Employee
{
    public string Name { get; set; }

    static void Main(string[] args)
    {
        int num = 1;
        Employee emp = new Employee();
        if (emp.GetType() == typeof(Employee))
        {
            Console.WriteLine(emp.GetType());
            Console.WriteLine(num.GetType());
            //Console.WriteLine(Employee.GetType()); // throws compile time error
            //Console.WriteLine(int.GetType()); // throws compile time error

            Console.WriteLine(typeof(Employee));
            Console.WriteLine(typeof(int));
            //Console.WriteLine(typeof(emp)); // throws compile time error
            //Console.WriteLine(typeof(num)); // throws compile time error
        }

        Type type = System.Type.GetType("System.Int64");
        Console.WriteLine(type.Name);
        Console.ReadKey();
    }
}

Output

Type Checking in C#

A C# type can be obtained using both typeof and GetType() methods.

An object can be cast to a specific type at runtime using the is operator. It is called runtime type identification. Depending on whether the object type matches the specified type, it returns true, otherwise it returns false.

Employee emp = new Employee();
if (emp is Employee)
{
    Console.WriteLine(emp.Gettype());
}

If the left hand side object matchs the given type, C# 7 uses the is operator to do safe casting.

Employee emp = new Employee();
if (emp.Name is string employeeName)
{
    Console.WriteLine($"Hello {employeeName}");
}

Below is an example of using is operator in C#.

class Employee
{
    public string Name { get; set; }

    static void Main(string[] args)
    {
        Employee emp = new Employee();
        emp.Name = "Rajanikant";
        if (emp is Employee)
        {
            Console.WriteLine(emp.GetType());

            if (emp.Name is string employeeName) // introduced in C# 7
            {
                Console.WriteLine($"Hello {employeeName}");
            }
        }
        Console.ReadKey();
    }
}

Output

Type Checking in C#

Table of Contents

Summary

In this article, we have learned Type Checking in C#

Leave a Reply

Related Posts

  • c# Exception Tostring vs Message – Complete Guide

  • c# Yield Exception Handling – Explained!

  • c# Excel Error 0x800a03ec – Complete Guide

  • c# get Error Message from Modelstate – Explained & Solved

  • c# Error Netsdk1005 – Solved!

  • c# Error Parsing Infinity Value – Explained & Solved!