Type Checking

Type describes data types. The .NET Runtime defined a type called System.Type, which is a representation of type in the System. Type stores the type information in a variable, property or field. Each and every class that has been defined in the system has a corresponding System.Type.

Determine type of a variable

Getting datatype of a variable in c# asp.net vb.net

All types in .Net are represented at runtime with an instance of System.Type. You will get all super classes, methods, properties fields, etc by calling the appropriate operations on System.Type. There are two basic ways to get System.Type object: call GetType on instance, or use the typreof operator on a type name.

typeof operator

The typeof operator is used to obtain the System.Type object for a type. It is often used as a parameter or as a variable or field. It is used to perform a compile time lookup i.e. given a symbol representing a Class name, retrieve the Type object for it.

C#

System.Type type = typeof(int);
Console.WriteLine(type);

VB.Net

Dim type As System.Type = GetType(Integer)
Console.WriteLine(type)

Output : System.Int32

GetType Method

GetType is a virtual method on Object, this means given an instance of a class, you can retrieve the exact runtime type of the current instance. Runtime type is the type of an object in memory. It is therefore a run-time concept. This is the type returned by the GetType() method.

C#

int i=100;
Console.WriteLine(i.GetType().FullName);

VB.Net

Dim i As Integer = 100
Console.WriteLine(i.[GetType]().FullName)

Output : System.Int32

Checking for variable type

The following example will clear about the usage of GetType Method and typeof operator.

C# Source Code

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 100;
            Console.WriteLine(i.GetType().FullName);
            Console.WriteLine(typeof(int).Name);
            if (i.GetType() == typeof(int))
                Console.WriteLine("Both are same");
            else
                Console.WriteLine("Both are not same");
            Console.ReadKey();
        }
    }
}

Final Output : Both are same

VB.Net Source Code

Module Module1
    Sub Main()
        Dim i As Integer = 100
        Console.WriteLine(i.[GetType]().FullName)
        Console.WriteLine(GetType(Integer).Name)
        If i.[GetType]() Is GetType(Integer) Then
            Console.WriteLine("Both are same")
        Else
            Console.WriteLine("Both are not same")
        End If
        Console.ReadKey()
    End Sub
End Module

Final Output : Both are same

When and where to use GetType() or typeof

GetType() works at runtime and typeof() is a compile-time operator.

Various Ways to Check Object Types  c# asp.net

GetType() is a method you can call on individual objects, to get the execution-time type of the object. Use GetType() when you want to get the type at execution time. It is a method of the object class that can be used on an instance.

Use typeof when you want to get the type at compilation time. It is an operator to obtain a type known at compile-time (or at least a generic type parameter). The operand for typeof is always the name of a type or type parameter. It can't be a variable or anything like that. It is calculated at compile time and thus cannot be used on an instance, which is created at runtime.

In both cases the result is an object of the type System.Type containing meta-information on a type.

Example:

using System;
namespace ConsoleApplication1
{
    class Car { }
    class Ford : Car { }
    class Program
    {
        static Car newCar()
        {
            return new Ford();
        }
        static void Main(string[] args)
        {
            Car c = newCar();
            Console.WriteLine(typeof(Car));     //Output : CAR
            Console.WriteLine(c.GetType());   //Output : FORD
            Console.ReadKey();
        }
    }
}

In the above example, within the Main() method, you are dealing with instances of type Car; so, if you care about the declared type, you would use typeof(Car). However, the newCar() method actually returns an instance of a derived class, Ford, despite declaring the base class as the return type. If you want to find out about this runtime type, call GetType on the returned instance.

GetType and TypeOf confusion

typeof vs Object.GetType() performance

From the above example, we should clear about the typeof operator is used when you want to get the Type instance representing a specific type. GetType() method gives the runtime type of the object on which it is called, which may be different from the declared type.


NEXT.....Aautoscroll multiline TextBox