C# GetType方法:获取对象type类型

  • 内容
  • 评论
  • 相关

C# GetType 方法用于获取当前实例的类型,返回值为 System.Type 类型。

C# GetType 方法不含任何参数,是非静态方法。

其调用与上一节《C# GetHashCode方法》介绍的 GetHashCode() 方法类似,使用任何对象都能直接调用该方法。

下面通过实例来演示该方法的使用。

【实例】创建字符串类型的变量、整数类型的变量以及 Student 类的对象,并分别使用 GetType 方法获取其类型并输出。

根据题目要求,代码如下。

class Program
{
    static void Main(string[] args)
    {
        int i = 100;
        string str = "abc";
        Student stu = new Student();
        Console.WriteLine(i.GetType());
        Console.WriteLine(str.GetType());
        Console.WriteLine(stu.GetType());
    }
}

执行上面的代码,效果如下图所示。

GetType方法的使用

本文标题:C# GetType方法:获取对象type类型

本文地址:http://www.hosteonscn.com/4818.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注