| title | ms.date | ms.prod | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
typeof (C# Reference) |
2015-07-20 |
.net |
|
article |
|
|
|
0c08d880-515e-46bb-8cd2-48b8dd62c08d |
21 |
BillWagner |
wiwagn |
|
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 xref:System.Object.GetType%2A, 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 xref:System.Collections.Generic.IEnumerable%601. Assume that method is an instance of a MethodInfo type:
string s = method.ReturnType.GetInterface
(typeof(System.Collections.Generic.IEnumerable<>).FullName);
Example
[!code-cscsrefKeywordsOperator#12]
Example
This sample uses the xref:System.Object.GetType%2A 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.
[!code-cscsrefKeywordsOperator#13]
C# Language Specification
[!INCLUDECSharplangspec]
See Also
xref:System.Type?displayProperty=nameWithType
C# Reference
C# Programming Guide
C# Keywords
is
Operator Keywords