C#中获取当前实例的类型

csharpserver side programmingprogramming更新于 2025/5/20 18:07:17

获取当前实例的类型,代码如下 −

示例

using System;
public class Demo {
   public static void Main(){
      string s = "Demo";
      Console.WriteLine("String = " +s);
      Console.WriteLine("String Type = " +s.GetType());
   }
}

输出

这将产生以下输出 −

String = Demo
String Type = System.String

示例

现在让我们看另一个例子 −

using System;
public class Demo {
   public static void Main(){
      double val1 = 5.5;
      int val2 = 10;
      short val3 = 2;
      Console.WriteLine("Value = " +val1);
      Console.WriteLine("Value Type = " +val1.GetType());
      Console.WriteLine("Value = " +val2);
      Console.WriteLine("Value Type = " +val2.GetType());
      Console.WriteLine("Value = " +val3);
      Console.WriteLine("Value Type = " +val3.GetType());
   }
}

输出

这将产生以下输出 −

Value = 5.5
Value Type = System.Double
Value = 10 Value
Type = System.Int32
Value = 2 Value
Type = System.Int16

相关文章