Type Checking in C#

In this article, you will learn about type checking in C#.

typeof is operator, It is used to find and return the type of built-in types like int, float, etc and user-defined types like class, interface, delegate, struct.

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

typeof does not allow to pass built-in type variable and user-defined types objects.

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

typeof checks type at compile time.

GetType() method is used to find and return built-in type objects and user-defined type objects.

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

GetType() method does not available on built-in types and user-defined types level.

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

There are two types of GetType() methods one is static method and another one is instance method, static GetType() method takes fully qualified name like Namespace.TypeName, if we do not pass namespace it will throw runtime 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#

Both typeof and GetType() method are used to get the type in C#.

The is operator is called runtime type identification, is operator is used to check if an object can be cast to a specific type at runtime. It returns Boolean value, if object type is match with specified type it returns true else it will return false.

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

In C# 7, is operator is also used to do safe casting, if left hand side object is match with given type.

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#

Summary

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


Similar Articles