Asp.net C #3.0 new feature learning (2): For more information about anonymous type and extension methods, see.
In the past two days, I took a look at the visual studio 2008 series of courses on msdnwebcast and recorded what I learned to deepen my memory.
1. Anonymous type
As the name implies, an anonymous type is a type without a name. In C #3.0, we can declare a temporary type in the program to store data, for example:
The Code is as follows: |
|
Class Program { Static void Main (string [] args) { // Declare an anonymous object with the Name and Age attributes Var obj = new {Name = "Joey", Age = 25}; // The new {Name = "Joey", Age = 25} Here is an anonymous type, obj is an object of this type, called an anonymous object. Console. WriteLine ("anonymous object obj: Name =" + obj. Name + ", Age =" + obj. Age ); } } |
In the above Code, an anonymous object obj is declared and the attribute value of the object is output. If you move the mouse over var in front of obj in VS, vs will prompt that obj is an anonymous type 'a. This 'a is a type automatically identified by the compiler. When an anonymous object is compiled, the compiler still has to give it a type. In fact, the above anonymous type new {Name = "Joey", Age = 25} is inherited directly from the Object, which is equivalent
The Code is as follows: |
|
Public class 'a { Public string Name {get; private set ;} Public int Age {get; private set ;} } |
Such a custom type with read-only attributes.
In MSDN, the anonymous type is defined as follows:
1. The anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type.
2. the type name is generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler.
3. You can use the new operator and object initial value to create an anonymous type.
The above three sentences are called "talking about ideas". Let people understand the anonymous type at a glance. However, I have to mention the relationship between the anonymous type and the declared keyword of var implicit type. Many new users think this is an anonymous object when they see the var declaration, objects of the anonymous type must be declared using var, but not all objects declared using var are anonymous objects, such as var n = 5. You cannot say that n is an anonymous object, n is just an implicitly typed local variable, while var s = new {S1 = "abc", S2 = "def"}; s is an object of the anonymous type. That is to say, an anonymous object is a temporary type object declared in memory using var. Its type cannot be inferred based on the expression of the instance on the right like the implicit type. It is a real anonymous type, and var I = 5; this implicit type declaration, during compilation, I is actually int32 type, and the implicit type is just a syntactic sugar.
Under what circumstances is the anonymous type generally used?
1. The anonymous type is usually used in the select clause of the query expression to return the attribute subset of each object in the source sequence (more than one object is used in Linq)
The anonymous type contains one or more public read-only attributes. A class member (such as a method or event) of another type is invalid. The expression used to initialize the attribute cannot be null, anonymous function, or pointer type.
The most common solution is to use other types of attributes to initialize the anonymous type. In the following example, assume that the class named Product exists. Class products include the Color and Price attributes, and other attributes that you are not interested in. The products variable is a collection of Product objects. The anonymous type declaration starts with the new keyword. The Declaration initializes a new type that only uses the two attributes of Product. This causes a small amount of data to be returned in the query.
If you do not specify the member name in the anonymous type, the compiler will specify the same name as the attribute used to initialize these members for the anonymous type members. The name must be provided for the attribute initialized using the expression, as shown in the following example. In the following example, the attribute names of the anonymous type are both Color and Price.
Var productQuery = from prod in products
Select new {prod. Color, prod. Price };
Foreach (var v in productQuery)
{
Console. WriteLine ("Color = {0}, Price = {1}", v. Color, v. Price );
}
In the preceding query, an anonymous object is used to save the query results. The new {prod. Color, prod. Price} anonymous type uses the Color attribute and Price attribute of the prod object in the query result as the attributes of the new anonymous object.
You can create an array of anonymous elements by combining the locally typed variables with the implicitly typed array, as shown in the following example.
Var anonArray = new [] {new {name = "apple", diam = 4}, new {name = "grape", diam = 1 }};
The code above stores two anonymous objects in an anonymous array. Note that in the code above, the attribute type and number of attributes in the initialization tool of the anonymous object stored in the anonymous array must be consistent. That is to say
New {name = "apple", diam = 4} = new {name = "grape", diam = 1} is created on the following conditions: the type and number of attributes of attribute names and stored values must be the same. vs compiler considers these two anonymous types to be the same type. In this way, the two anonymous types can be stored in an anonymous array such as anonArray. Because anonymous Arrays can only store the same type of anonymous objects.
2. Extension Method
1. Extension method, which can be used to expand existing class functions so that instances of this type have more methods (functions ). For example, if you obtain a dll assembly from a third-party vendor, and we want to expand a write function for the Assembly class, we can use the extension method to expand the class.
2. Extension Method only looks like a type Method, but it is essentially not. It is more like a static Method of the static type. In fact, it does have all the functions of the static Method. This will be understood in the following implementation of the extension method, in fact, the extension method is a static method.
3. The Extension Method scope is visible to the entire namespace. You can use the using namespace to import the Extension Method from other namespaces.
4. The intermediate language (IL) generated by the compiler converts the code into a call to a static method. Therefore, it does not really violate the encapsulation principle.
5. In fact, extension methods cannot access private variables in their extended types.
Conventions:
1. You can use the extension method to extend the class or interface, but you cannot override the extension method.
2. Extension methods with the same name and signature as interfaces or class methods will never be called.
3. During compilation, the priority of the extension method is always lower than the instance method defined in the type itself.
In other words, if a type has a method named Process (int I) and you have an extension method with the same signature, the compiler always binds the instance method.
4. When the compiler encounters a method call, it first looks for a matching method in the instance method of this type.
If no matching method is found, the compiler searches for any extension method defined for this type and binds it to the first extension method it finds.
This is defined in MSDN:
The extension method enables you to "add" methods to an existing type without creating a new derived type, re-compiling, or modifying the original type in other ways. An extension method is a special static method, but it can be called like an instance method of an extension type.
The Code is as follows: |
|
{ Class Program { Static void Main (string [] args) { Var stu = new Student () {Name = "joey", Age = 25 }; // Call the instance method Console. WriteLine (stu. ToString ()); // Call the extension method. During the call, vs smart sensing adds a downward arrow to the method to indicate that this is an extension method. Console. WriteLine (stu. Hello ()); } } Public class Student { Public string Name {get; set ;} Public int Age {get; set ;} // Rewrite the ToString Method Public new string ToString () { Return "Name:" + this. Name + "nAge:" + this. Age; } } Public static class ExtendMehods { // Use the this keyword to extend the Class Method Public static string Hello (this Student stu) { Return "Hi! Hello everyone! My Name is "+ stu. Name +". I am "+ stu. Age +" years old "; } } } |
In fact, the extension method is a static method in a static non-generic class. You can also use static class name points to call the method.