14.5.11 The typeof operator

本文详细介绍了C#中typeof运算符的使用方法及其返回的System.Type对象的意义。包括如何获取不同类型(如int、string及数组类型)的Type对象,并通过示例展示了不同类型的名称输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值