. Net wildcard local type, attribute accessors protection level, namespace alias qualifier
Generic 1> = Local type:
In c #1.0, a class can only be placed in one file. C #2.0 uses a keyword <partial> to divide a class into two parts <that is, the implementation of a class can be in multiple files>. The compilation result is exactly the same as that written in a file. More significance lies in engineering management.
... 1> Local type values are applicable to interfaces, classes, or structures. enumeration is not supported. <generally, enumeration is not as large as that.>;
.. 2> each part of the same type must have a modifier partial, which must be in the same namespace and must be compiled and accessed simultaneously;
... 3> the keyword partial is a context keyword. It indicates a keyword only when it is put together with class, struct, and interface;
.. 4> the features and interfaces of local types have the "accumulate" effect;
.. 5> If a part of a type uses the abstract modifier, the entire class will be treated as an abstract class;
.. 6> If a part of a type uses the sealed modifier, the entire class is regarded as a sealed class;
.. 7> conflicting modifiers cannot be used for each part. For example, abstract cannot be used for one part, and sealed can be used for another part;
... 8> the base classes specified on each part must be consistent. Some parts do not specify the base class, but if they are specified, they must be the same.
2> = attribute accessors protection level:
C #2.0 allows different access level modifiers for get and set accessors of an attribute <Note the concept of attribute accessors and attributes>.
... 1> the access modifier on the property accessor (get or set) must be stricter than the access modifier of the property;
.. 2> You can only specify the access modifier on one property accessor (get or set), and the other uses the access modifier of the property by default;
.. 3> the property in the interface can only be public by default, and the access modifier of the property accessor (get or set) cannot be set.
3 >= namespace alias qualifier <:>:
C #2.0 allow the namespace alias qualifier (: :) to avoid conflicts of type names in different namespaces. When the namespace alias qualifier (: :) is used, the compiler can ensure that this is a qualifier only applicable to the "namespace alias". <check that the delimiter ':'> is used in the il code, it is not classified as another type or a member qualifier (.). The keyword global can be placed on the left of the namespace alias qualifier (: :), which allows the compiler to only search for all namespaces, rather than other types or members. Use the namespace alias qualifier (: :) as much as possible, and reduce the use of general qualifiers such as dot (.) <accelerate the compiler search type, no difference after compilation>.
1> = generic Overview:
Generic is a type of polymorphism. For example, when we write a stack or queue, We need to specify its data type, int code and string code, an object code. Except for different data types, most of the Code is the same. According to the design pattern, the change point is abstracted to encapsulate it, and the common part is used as the common code. The change here is the type, and the common part is that the algorithm is the same, so the type is abstracted, so the generic model is available <personal understanding>.
C # generics are supported by clr at runtime, which enables seamless collection of generics in various languages supported by clr; c # When the <First compilation> generic code is compiled as il code and metadata [generic version of il and metadata], special Placeholders are used to represent generic types, use proprietary il commands to support generic operations. The real generic instantiation occurs during jit compilation <Second compilation>. When the jit compiler encounters this special il and metadata for the first time, it will replace <generic type instantiation> with the actual type. Clr generates the same code for all generic types with "reference type" parameters. For value types, different value types generate different codes, if they are the same, they share the same code.
C # generic types carry rich metadata. Therefore, c # generic types can be applied to powerful reflection technologies. They use "basic classes, interfaces, constructors, value Type/reference type constraints to achieve "Explicit Constraints" on type parameters, improving type security.
Source code:
Mytype 1 public class mytype <t> where t: struct {
2 private t [] _ items;
3 public void add (t itme)
4 {
5}
6}
After compilation, il is as follows:
Il 1 // generic class <'1 indicates the number of elements or number of parameters>
2. class public auto ansi beforefieldinit mytype '1 <valuetype. ctor
3 // note that the wildcard constraint <[mscorlib] system. valuetype) t> is added here.
4 // indicates that the type parameter is of the Value Type
5 ([mscorlib] system. valuetype) t>
6 extends [mscorlib] system. object
7 {
8} // end of class mytype '1
9 // This is the private field
10. field private! T [] _ items
11
12 // add method, type parameter <t> there is an exclamation point before <!>, This is the beginning of the rule to support generics.
13 // The new feature introduced later. It indicates the existence of the first type parameter specified for the class, indicating that this is
14 // a type parameter
15. method public hidebysig instance void add (! T itme) Pencil managed
16 {
17 // code size 2 (0x2)
18. maxstack 8
19 il_0000: nop
20 il_0001: ret
21} // end of method mytype '1: add
In addition to these differences, there is no big difference between the generic and non-generic types in the template code.
2 >= generic type and generic method:
Generic types include classes, interfaces, structures, and delegation.
C # supports generic methods, but does not support other members except methods <attributes, events, indexers, constructors, and Destructors>; generic methods can be included in generic types, or in non-generic types.
Genericsmehod 1 public class genericsmehod
2 {
3 // generic methods in non-generic classes. The parameter constraints are reference types.
4 // <input value type parameters will cause compilation errors>
5 public int finditem <t> (t [] items, t item) where t: class
6 {
7 for (int I = 0; I <items. length; I ++)
8 {
9 if (items [I]. equals (item ))
10 {
11 return I;
12}
13}
14 return-1;
15}
16}
You do not need to write for the call. generic methods support overloading, but the overload of the parameter constraints of different types is invalid. It also supports rewriting. The type parameter constraints during rewriting are inherited by default, it is unnecessary to specify any constraints.
3> = generic constraints:
Why are there constraints? If I write a generic class, this generic parameter calls the compareto method, but not all type parameters have this method, if the input type does not have this method, this will cause errors. To ensure the robustness of your code, it is necessary to add constraints <that is to say, the input type must have this method before compilation can pass, expose errors in the compilation phase>. Generic constraints support four types of constraints: interface constraints, base class constraints, constructor constraints, value type constraints, and reference type constraints. constraints are not mandatory. If no constraints are specified, the type parameter can only access system. public methods in object type. Syntax where statement
The preceding type parameter requires a compareto method and can be implemented using an interface constraint <public class mygenerics <t> where t: icomparable {}>;
Base class constraints: If a table is a type parameter, it must be an inherited sub-specified type <where t: base class>;
Constructor constraints: Only constructor constraints without parameters are supported. It must ensure that the parameter type can be called by its non-parameter constructor <where t: new ()>:
Value Type/reference type constraints: there are only two cases: <where t: struct> or <where t: class>. The specified parameter type must be a value or reference type.