c语言中typeof,运算符关键字typeof的使用

运算符关键字typeof的使用

引导语:C语言是一种计算机程序设计语言,它既具有 高级语言的特点,又具有 汇编语言的特点。以下是小编整理的运算符关键字typeof的'使用,欢迎参考阅读!

用于获取类型的 System.Type 对象。typeof 表达式采用以下形式:

System.Type type = typeof(int);

备注

若要获取表达式的运行时类型,可以使用 .NET Framework 方法 GetType,如以下示例中所示:

int i = 0;

System.Type type = i.GetType();

不能重载 typeof 运算符。

typeof 运算符也能用于公开的泛型类型。具有不止一个类型参数的类型的规范中必须有适当数量的逗号。下面的示例演示如何确定方法的返回类型是否是泛型 IEnumerable)>)。假定此方法是 MethodInfo 类型的实例:

string s = method.ReturnType.GetInterface

(typeof(System.Collections.Generic.IEnumerable<>).FullName

示例

C#

public class SampleClass2

{

public int sampleMember;

public void SampleMethod() {}

static void Main()

{

Type t = typeof(SampleClass);

// Alternatively, you could use

// SampleClass obj = new SampleClass();

// 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:

System.Type GetType()

System.String ToString()

Boolean Equals(System.Object)

Int32 GetHashCode()

Members:

System.Type GetType()

System.String ToString()

Boolean Equals(System.Object)

Int32 GetHashCode()

Void .ctor()

Void .ctor(Int32, System.String)

System.String name

Int32 id

*/

此示例使用 GetType 方法确定用来包含数值计算的结果的类型。这取决于结果数字的存储要求。

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

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值