var is a 3.5 new definition of the type of the variable is actually the weakening type of the definition of Var can be substituted for any type of compiler will be based on the context to determine what type you want to use in the case of Var I think you can not be sure what type you will use var similar Object but efficiency is higher than object.
Or in layman's words:
var can be understood as an anonymous type, and we can think of it as a placeholder for declaring a variable. It is primarily used when declaring a variable, when the data type cannot be determined.
The following four characteristics are used when defining variables with Var:
1. Must be initialized at the time of definition. It must be var s = "ABCD" form, not the following form: Var s; s = "ABCD";
2. Once initialization is complete, it is not possible to assign a value that differs from the initialization value type.
3. The var requirement is a local variable.
4. Using var to define variables is different from object, which is exactly the same as defining variables in terms of efficiency and using strongly typed methods.
Instance:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacefirstpriject{classProgram {Static voidMain (string[] args) { //declaring and initializing variables varName ="Zhang San"; varAge =Ten; varSex =true; //get the data type of a variableType T_name =name. GetType (); Type T_age=Age . GetType (); Type T_sex=sex. GetType (); //Print ResultsConsole.WriteLine ("the type of the variable name is {0}, the type of the variable age is {1}, and the type of the variable sex is {2}", T_name.tostring (), t_age.tostring (), t_sex.tostring ()); //do not automatically close the console, wait for inputConsole.ReadLine (); } }}
View Code
Example result diagram:
This article quoted: http://wenda.so.com/q/1378644256068714
This article is based on the <<vs2010 of Baidu lessons easily learn c#-from zero to in-depth-net4 fun programming video Tutorials >> making corresponding notes.
The var keyword in C #