要获取当前实例的类型,代码如下-
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