Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
 typeof
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
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);

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<(Of <(T>)>). Assume that method is an instance of a MethodInfo type:

string s = method.ReturnType.GetInterface
    (typeof(System.Collections.Generic.IEnumerable<>).FullName);
C#
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
*/

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.

C#
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
*/

For more information, see the following sections in the C# Language Specification:

  • 7.5.11 The typeof operator

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
"See Also" link to System.Type      Cornan The Iowan   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
GetType Example - Using PowerShell      Thomas Lee   |   Edit   |   Show History
<#
.SYNOPSIS
This script displays the value, and type, of an expression.
.DESCRIPTION
This script is a rewrite of the second example on this page, The
script illustrates how to use the GetType method to return
the type that results from a calculation. in this case, two
multiplying two int32 with the Pi field results in a System.Double.
.NOTES
File Name : get-typetest.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2 CTP3
.LINK
This script posted to:
http://pshscripts.blogspot.com/2009/06/get-typetestps1.html
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/58918ffs.aspx
.EXAMPLE
PSH [C:\foo]: .\get-typetest.PS1
The type of $Radius is : System.Int32
Area = 28.2743338823081
The type is the expression is: System.Double
#>
##
# Start of Script
##

# Set and display type of radius
[int] $radius = 3
"The type of `$Radius is : {0}" -f $radius.GetType()
# Display Area, and the type of an expression.
"Area {0}" -f ($radius * $radius * [System.Math]::PI)
"The type is the expression is: {0}" -f ($radius * $radius * [System.Math]::PI).GetType()
# End of Script
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker