typeof (C# Reference)

Used to obtain the System.Type object for a type. A typeof expression takes the following form:

System.Type type = typeof(int);

Remarks

To obtain the run-time type of an expression, you can use the .NET Framework method GetType, as in the following example:

int i = 0;
System.Type type = i.GetType();

The typeof operator cannot be overloaded.

The typeof operator can also be used on open generic types. Types with more than one type parameter must have the appropriate number of commas in the specification. The following example shows how to determine whether the return type of a method is a generic IEnumerable<T>. Type.GetInterface will return null if the return type is not an IEnumerable<T> generic type.

using System;
using System.Collections.Generic;
using System.Reflection;

class Example
{
    static void Main()
    {
        MethodInfo method = typeof(string).GetMethod("Copy");
        Type t = method.ReturnType.GetInterface(typeof(IEnumerable<>).Name);
        if (t != null)
            Console.WriteLine(t);
        else
            Console.WriteLine("The return type is not IEnumerable<T>.");
        /*
        Output:
        System.Collections.Generic.IEnumerable`1[System.Char]
        */
    }
}

Example

public class ExampleClass
{
   public int sampleMember;
   public void SampleMethod() {}

   static void Main()
   {
      Type t = typeof(ExampleClass);
      // Alternatively, you could use
      // ExampleClass obj = new ExampleClass();
      // Type t = obj.GetType();

      Console.WriteLine("Methods:");
      System.Reflection.MethodInfo[] methodInfo = t.GetMethods();

      foreach (System.Reflection.MethodInfo mInfo in methodInfo)
         Console.WriteLine(mInfo.ToString());

      Console.WriteLine("Members:");
      System.Reflection.MemberInfo[] memberInfo = t.GetMembers();

      foreach (System.Reflection.MemberInfo mInfo in memberInfo)
         Console.WriteLine(mInfo.ToString());
   }
}
/*
 Output:
    Methods:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Members:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Void .ctor()
    Int32 sampleMember
*/

Example

This sample uses the GetType method to determine the type that is used to contain the result of a numeric calculation. This depends on the storage requirements of the resulting number.

class GetTypeTest
{
    static void Main()
    {
        int radius = 3;
        Console.WriteLine("Area = {0}", radius * radius * Math.PI);
        Console.WriteLine("The type is {0}",
                          (radius * radius * Math.PI).GetType()
        );
    }
}
/*
Output:
Area = 28.2743338823081
The type is System.Double
*/

C# language specification

For more information, see The typeof operator in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also