You can use var to declare a variable. The declared variable type is determined by the type of the variable value initialized,
This is a bit like the variant in VB6.0.
Class Program {static void Main (string [] args) {// declare with the var keyword, var I = 0; // declare that the class Program must be initialized // var I; // cannot be empty initially // var I = null; Console. writeLine (I); // value can be assigned again // I = 10; // value cannot be assigned to another type // I = "0"; Console. writeLine (I); // using var declaration can be assigned to different types of values. Its type is determined by the type of the initialized value var s = "Hello C #3.0"; Console. writeLine (s); // declared array var num = new [] {0, 1, 2}; foreach (int n in num) {Console. writeLine (n);} // You can also output foreach (var n in num) {Console. writeLine (n);} // declared object var student = new {Name = "Xiao qiushui", Age = 23}; Console. writeLine ("Hero {0}", student. name) ;}} class TestVar {// class member cannot be declared // var s = "abc"; public void TestMethod () {// you can declare the method local variable var n = 10 ;}}
Summary:
1. Use var to declare a variable. The declared variable type is determined by the type of the variable value initialized.
2. The declaration must be initialized and cannot be empty initially.
3. This variable cannot be assigned another value of the data type.
4. An array can be declared.
5. objects that can be declared
6. Class Members cannot be declared. Only local variables can be used.