在C#中,您可以使用typeof
关键字来获取变量的数据类型。typeof
是一个运算符,它返回一个表示数据类型的System.Type
对象。以下是一个示例:
using System; class Program { static void Main() { int myInt = 42; double myDouble = 3.14; string myString = "Hello, World!"; Console.WriteLine("Type of myInt: " + typeof(myInt)); Console.WriteLine("Type of myDouble: " + typeof(myDouble)); Console.WriteLine("Type of myString: " + typeof(myString)); } }
输出:
Type of myInt: System.Int32 Type of myDouble: System.Double Type of myString: System.String