在C#中,var是关键字,用于声明隐式类型变量,该变量根据初始化值指定变量的类型。
语法:
var variable_name = value;
using System; using System.Text; namespace Test { class Program { static void Main(string[] args) { var a = 10; var b = 10.23; var c = 10.23f; var d = 10.23m; var e = 'X'; var f = "Hello"; Console.WriteLine("value of a {0}, type {1}", a, a.GetType()); Console.WriteLine("value of b {0}, type {1}", b, b.GetType()); Console.WriteLine("value of c {0}, type {1}", c, c.GetType()); Console.WriteLine("value of d {0}, type {1}", d, d.GetType()); Console.WriteLine("value of e {0}, type {1}", e, e.GetType()); Console.WriteLine("value of f {0}, type {1}", f, f.GetType()); //按ENTER退出 Console.ReadLine(); } } }
输出结果
value of a 10, type System.Int32 value of b 10.23, type System.Double value of c 10.23, type System.Single value of d 10.23, type System.Decimal value of e X, type System.Char value of f Hello, type System.String