14.5.11 The typeof operator
来源:互联网 发布:win7系统内存优化 编辑:程序博客网 时间:2024/06/06 03:43
The typeof operator is used to obtain the System.Type object for a type.
typeof-expression:
typeof ( type )
typeof ( void )
The first form of typeof-expression consists of a typeof keyword followed by
a parenthesized type. The
result of an expression of this form is the System.Type object for the
indicated type. There is only one
System.Type object for any given type. [Note: This means that for type T,
typeof(T) == typeof(T)
is always true. end note]
The second form of typeof-expression consists of a typeof keyword followed
by a parenthesized void
keyword. The result of an expression of this form is the System.Type object
that represents the absence of
a type. The type object returned by typeof(void) is distinct from the type
object returned for any type.
[Note: This special type object is useful in class libraries that allow
reflection onto methods in the language,
where those methods wish to have a way to represent the return type of any
method, including void methods,
with an instance of System.Type. end note]
Chapter 14 Expressions
151
[Example: The example
using System;
class Test
{
static void Main() {
Type[] t = {
typeof(int),
typeof(System.Int32),
typeof(string),
typeof(double[]),
typeof(void) };
for (int i = 0; i < t.Length; i++) {
Console.WriteLine(t[i].FullName);
}
}
}
produces the following output:
System.Int32
System.Int32
System.String
System.Double[]
System.Void
Note that int and System.Int32 are the same type. end example]
typeof-expression:
typeof ( type )
typeof ( void )
The first form of typeof-expression consists of a typeof keyword followed by
a parenthesized type. The
result of an expression of this form is the System.Type object for the
indicated type. There is only one
System.Type object for any given type. [Note: This means that for type T,
typeof(T) == typeof(T)
is always true. end note]
The second form of typeof-expression consists of a typeof keyword followed
by a parenthesized void
keyword. The result of an expression of this form is the System.Type object
that represents the absence of
a type. The type object returned by typeof(void) is distinct from the type
object returned for any type.
[Note: This special type object is useful in class libraries that allow
reflection onto methods in the language,
where those methods wish to have a way to represent the return type of any
method, including void methods,
with an instance of System.Type. end note]
Chapter 14 Expressions
151
[Example: The example
using System;
class Test
{
static void Main() {
Type[] t = {
typeof(int),
typeof(System.Int32),
typeof(string),
typeof(double[]),
typeof(void) };
for (int i = 0; i < t.Length; i++) {
Console.WriteLine(t[i].FullName);
}
}
}
produces the following output:
System.Int32
System.Int32
System.String
System.Double[]
System.Void
Note that int and System.Int32 are the same type. end example]
- 14.5.11 The typeof operator
- typeof operator
- 14.5.10 The new operator
- overload the operator
- Explaining the PeopleCode @ operator
- qmake "The *= operator"
- perl the special operator
- The sizeof Operator
- The in operator
- 14.9.9 The is operator
- 14.9.10 The as operator
- The "tr///" operator in Perl
- VC++[The Interesting Logic Operator]
- C# Snippet Tutorial - The ?? Operator
- The Type Comparison Operator instanceof
- The Type Comparison Operator instanceof
- The operator of C++ : sizeof
- typeof
- 14.5.9 Postfix in- & decrement operators
- 14.5.10 The new operator
- 14.5.10.1 Object creation expressions
- 14.5.10.2 Array creation expressions
- 14.5.10.3 Delegate creation expressions
- 14.5.11 The typeof operator
- 14.5.12 The checked & unchecked operators
- Qomolangma实现篇(二):命名空间和别名子系统的实现
- Qomo OpenProject Field Test 2发布!
- 针对prototype中封装的几个Ajax对象做的用例
- 上周技术关注:Atlas实现机制浅析
- 最近比较懒散
- 最近在看周易
- 索引对查询条件的影响