Keywords: implicit type variable var, extension method (extension), lambda expression, auto attribute, anonymous type
One, implicit type
C # is a strongly typed language, meaning that we must specify the exact type of the variable when declaring it, but Var can replace the formal data type name (such as int, bool, string), and the compiler infers the data type of the variable based on the initial value used to initialize the local variable We can use implicit types for all types in the base class library, including arrays, generics, custom types.
Usage Restrictions:
1. You can only apply the declaration of local variables within the method or attribute, and you cannot use Var to define the return value, the type of the parameter, or the data member of the type;
2. Local variables declared with VAR must be assigned an initial value, and cannot be null as the initial value.
Strongly typed data: After an implicit type initialization, the compiler has inferred the exact type, so he is strongly typed.
Role: You can dynamically create result sets based on the format of the query itself, so that we do not need to display the types that the definition query might return
Second, automatic attributes
Because. NET base class libraries always use type properties instead of traditional access and modification methods, so. NET language recommends using type attributes to encapsulate private data fields instead of using the getxxx () and Setxxx () methods.
At the bottom, C # properties are mapped to methods that prefix get_ and set_, meaning that C # automatically generates the Get_name () and Set_name () methods if the Name attribute is defined.
When defining automatic attributes, you must provide both the get and set keywords.
Iii. types of anonymity
A class can express a given programming entity, we typically create a C # class that provides the necessary set of properties, methods, and events for reuse, and sometimes you might need to define a class to encapsulate some related data without requiring any associated methods, events, or the class to be reused across projects.
C # 3.0 provides anonymous types, using the New keyword var and the object initialization syntax described earlier, such as: var worker = new {FirstName = "Vincent", LastName = "Ke", level = 2}; The C # compiler automatically generates classes with unique names at compile time. Because the name of this class is not visible in C #, it is necessary to use the VAR keyword to use implicit typing.
Anonymous type internal representation:
Anonymous types are automatically inherited from System.Object, and can be called by the ToString (), GetHashCode (), Equals (), GetType () methods
C # 3.0 Language Features