The typeof Operator


The typeof operator obtains information about a type in the form of an object of the class System.Type. The characteristics of the type may be determined using the returned object. The syntax of the typeof operator is:

typeof(type)

where type is the data type which is being queried.

Once the Type object has been obtained, information can be gained through properties, fields and methods defined by Type. Type is a large class with many members. Two properties are FullName and IsClass. The property FullName returns the full name of the type in the form of a string. The property IsClass is a boolean property that returns true when the object is a class. Below is a program that makes use of these properties.

// Runtime4 - using typeof

using System;

namespace Runtime4
{
    class X { }

    class Program
    {
        static void Main()
        {
            Type t = typeof(X);
            Console.WriteLine(t.FullName);
            Console.WriteLine("Is Class: {0}", t.IsClass);
        }
    }
}

The output of the program is shown below.

Runtime4.X
Is Class: True

The full class name includes the name of the enclosing namespace, which in this case is Runtime4.