VaR I = 10; // implicitly typed
Int I = 10; // explicitly typed
As shown above, the VaR keyword can be used to define a variable. The type of this variable can be automatically identified by the compiler according to its initialization value during compilation. This can be used by somevar. getType (). NAME value for verification. In addition
For common types such as int, bool, and string variables, other arrays, list <t>, and user-defined objects can all be defined using VAR, use these types of values for initialization. For example
VaR evennumbers = new int [] {2, 4, 6, 8}; note that when defining an implicit type array, all types must be of the same type, because the compiler does not automatically treat different types of objects as system. object type.
VaR mycars = new list <car> ();
VaR mycar = new car ();
Note: var variables can only be defined as local variables in methods, but cannot be class member variables. It cannot be used in method parameters or return values.
For implicit type variables, if they cannot be identified, it makes no sense. Therefore, the VaR variable must be initialized at the same time during declaration and cannot be assigned a null value. However, if we have initialized a var variable, the compiler has determined that
The type of the variable. Then, the variable can be assigned a null value. According to the preceding rules, once a var variable is defined, the compiler determines its type at the same time. In the future, its type will be determined. You can
Type to give it a new value, you can also assign it to another value, or as the return value of the method or something. Similarly, once defined, the VaR variable can accept the null value.
However, there is another rule: Empty var variables cannot be defined. Because it can be null, it means that it can be initialized with null, while var variables cannot be initialized with null values.
A variable is not directly defined. It must be var-type, so that the person reading the code has to judge by himself. What is the significance of this variable? The meaning lies in the ability of the user to write data to a table. Furthermore, we recommend that you use implicit variables only when it comes to LINQ.