14.5.11 The typeof operator

王朝other·作者佚名  2006-02-28
窄屏简体版  字體: |||超大  

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]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航