阅读量:2
在C#中,你可以使用typeof关键字来获取一个类型的信息。typeof是一个运算符,它返回一个System.Type对象,该对象表示指定的类型。以下是一些使用typeof的示例:
using System;
class Program
{
static void Main()
{
// 获取内置类型的信息
Type intType = typeof(int);
Type doubleType = typeof(double);
Type stringType = typeof(string);
// 获取自定义类型的信息
Type myCustomType = typeof(MyCustomClass);
// 输出类型信息
Console.WriteLine($"Type of int: {intType}");
Console.WriteLine($"Type of double: {doubleType}");
Console.WriteLine($"Type of string: {stringType}");
Console.WriteLine($"Type of MyCustomClass: {myCustomType}");
}
}
class MyCustomClass
{
// 自定义类的定义
}
在这个示例中,我们首先获取了一些内置类型(如int、double和string)的信息,然后获取了一个自定义类型(MyCustomClass)的信息。最后,我们将这些类型信息输出到控制台。
以上就是关于“c# typeof怎样获取类型信息”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm