The var keyword in C,
Var is a new definition variable of 3.5. In fact, it is also a weakening type definition. VAR can replace any type. The compiler will judge what type you want to use based on context. when using VAR, I think that you cannot determine what type you will use. You can use VAR similar to an OBJECT, but the efficiency is higher than that of the OBJECT.
Or in other words:
Var can be understood as an anonymous type. We can think of it as a placeholder for declaring variables. It is mainly used when variables are declared and data types cannot be determined.
Using var to define variables has the following four features:
1. It must be initialized during definition. That is, it must be in the form of var s = "abcd", rather than the following: var s; s = "abcd ";
2. Once Initialization is complete, the variable cannot be assigned a value of a different type than the initialization value.
3. var must be a local variable.
4. Using var to define variables is different from object. It is the same in efficiency as defining variables using a strong type.
Instance:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace FirstPriject {class Program {static void Main (string [] args) {// declare and initialize the variable var name = "Zhang San"; var age = 10; var sex = true; // Type t_Name = name of the retrieved variable. getType (); Type t_Age = age. getType (); Type t_Sex = sex. getType (); // print the result Console. writeLine ("the variable name type is {0}, the variable age type is {1}, and the variable sex type is {2}", t_Name.ToString (), t_Age.ToString (), t_Sex.ToString (); // The Console is not automatically closed, waiting for the Console to be entered. readLine ();}}}
View Code
Instance result diagram:
This article cited from: http://wenda.so.com/q/1378644256068714
This article takes notes based on <VS2010 easy to learn C #-from scratch to deep-NET4 fun programming video tutorial> In the Baidu course.