In general, we declare variables by specifying the data type and identifier, as follows:
int myint=32;
You can also define it as follows,
int myint=myotherint*99; (Myotherint has been initialized)
In addition, you can ask the C # compiler to judge the type of a variable based on an expression, using the keyword VAR to declare it, as
var myvariance=99;
var myothervariance= "Hello";
Two variables myvariance and myothervariance calledImplicit type(Implicitly typed) variable. The VAR keyword instructs the compiler to infer the type of the variable based on the expression used to initialize it. In the previous example, the myvariance is an int type, and in the following example, the Myothervariance is a string type.
Notable points:
①,var only gives us the convenience of the variable declaration, once the declaration is finished, only the type of value inferred by the compiler can be assigned to him, for example, you cannot assign a float, double, string type to myvariable;
②, the keyword var can only be used when an expression is provided to initialize the variable, and the following declaration is illegal.
var yetanothervariable, not initialized
③,var encourages programmers to be lazy and makes programs more difficult to understand; In general, you should insist on using a variable of the type explicitly, unless you have to, do not use an implicit variable.