What is generics ("generics" in broad sense)--5 generic types in a generic-->c# in a specific language (C #)
1. What is a generic type?
2. Five generics are available in C # (generics in C #)
3. Generic class in C # (Generic Classes)
4. Generic method Generic Methods
5. Generic structure Generic Structs
6, generic commissioned generic Delegates
7. Generic interface Generic Interfaces
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/77/78/wKioL1Zox9PyeSuvAAEYIT9MnUQ414.png "title=" Generic.png "alt=" Wkiol1zox9pyesuvaaeyit9mnuq414.png "/>
=================
1. What is a generic type
Generics are used in an abstract way to achieve the reuse of methods. "Reuse of methods" is the purpose of generics, and its means is to abstract and to parameterize the "type".
There is times, when a class would is more useful if you could ' distill ' or ' refactor ' out of its actions and apply them not Just to the data types for which they is coded, but for other types as well.
Generics allow you to do just. You can refactor your code and add an additional layer of abstraction so, for certain kinds of code, the data types a Re not hard-coded. This was particularly designed for cases in which there was multiple sections of code performing the same instructions, but On different data types.
=================
2. Five generic types are available in C #
Generics allow developers to write "type-parameterized Code", which provides a container (placeholders for types) to hold the type type-parameterized.
The generics feature offers a more elegant the-a-to-a set of code with more than one type. Generics allow you to declare type-parameterized code, which you can instantiate with different types. This means you can write the ' Code with ' placeholders for types ' and then supply the actual types when you create an Instan Ce of the class.
C # provides 5 types of generics: classes, structs, interfaces, delegates, and methods.
C # provides five kinds of generics:classes, structs, interfaces, delegates, and methods. Notice The first four is types, and methods is members.
=================
3. Generic type (Generic Classes)
The process of creating and instantiating a generic class:
Generic class-->constructed type-->instances of the constructed Type
As you know, there is both steps for creating and using your own regular, nongeneric classes:declaring the class and Crea Ting instances of the class. However, generic classes is not actual classes and templates for classes-so you must first construct actual class types from them. You can then create references and instances from these constructed class types.
1. Declare a class, using placeholders for some of the types.
2. Provide actual types to substitute on for the placeholders. This gives a actual class definition with all the ' blanks ' filled in. This is called a constructed type.
3. Create instances of the constructed type.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/77/79/wKiom1ZoyCXiAJmJAABbX1MhBPM722.png "title=" Qq20151209212440.png "alt=" Wkiom1zoycxiajmjaabbx1mhbpm722.png "/>
3.1, declaring a Generic class (declares a generic class)
Declaring a simple generic class was much like declaring a regular class, with the following differences:
1. Use "<>" after the class name
2. Place the type parameter in <> (type parameters), separated by "," in the middle
3. Using the type parameter inside the generic class (type parameters)
1. A matching set of angle brackets after the class name.
2. Between the angle brackets, you place a comma-separated list of the placeholder strings that represent the types Supplied on demand. These is called type parameters.
3. The type parameters throughout the body of the declaration of the generic class to represent the types that sho Uld is substituted in.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/77/78/wKioL1ZoyMXgtfsMAABbx-5A7dM458.png "title=" Qq20151209213555.png "alt=" Wkiol1zoymxgtfsmaabbx-5a7dm458.png "/>
The difference between a generic class and a normal class is "<>"
There is no special keyword that flags a generic class declaration. Instead, the presence of the type parameter list, demarcated (set ... The line, differentiated) with angle brackets, distinguishes a generic class declaration from a regular class declaration.
3.2, Creating a constructed type (create constructed type)
Once you has declared the generic type, you need to tell the compiler what actual types should is substituted for the PLA Ceholders (the type parameters). The compiler takes those actual types and creates a constructed type, which is a template from which it creates actual CLA SS objects.
Type Arguments-->type Parameters
The real types being substituted for the type parameters is called type arguments.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/77/79/wKiom1ZoyK-QgK72AAAVgp3jlKM794.png "title=" Qq20151209214319.png "alt=" Wkiom1zoyk-qgk72aaavgp3jlkm794.png "/>
The compiler takes the type arguments and substitutes them for their corresponding type parameters throughout the body of The generic class, producing the constructed Type-from which actual class instances are created.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/77/78/wKioL1ZoyQaQ3ZsxAABRh_ValME861.png "title=" Qq20151209214337.png "alt=" Wkiol1zoyqaq3zsxaabrh_valme861.png "/>
3.3. Creating Variables and Instances
A constructed class type is used just like a regular type in creating references and instances.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/77/78/wKioL1ZoybOBnVx-AABQxgs6nIY836.png "title=" Qq20151209214914.png "alt=" Wkiol1zoybobnvx-aabqxgs6niy836.png "/>
Many different class types can be constructed from the same generic class. Each of the are a separate class type, just as if it had its own separate nongeneric class declaration.
3.4. Comparing the Generic and nongeneric Stack
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/77/79/wKiom1ZoyY-xeafwAAEj6OQamWk762.png "title=" Qq20151209220417.png "alt=" Wkiom1zoyy-xeafwaaej6oqamwk762.png "/>
3.5. Constraints on Type Parameters
Since the generic stack doesn ' t know the type of the items it'll be storing, it can ' t know what members these types Impl Ement.
All C # objects, however, is ultimately derived from class object, so the one thing the stack can is sure of about the ITE Ms It's storing is that they implement the members of class object. These include methods ToString, Equals, and GetType. Other than this, it can ' t know what is available.
To make generics more useful, you need to be able to supply additional information to the compiler about what kinds of typ ES is acceptable as arguments. These additional bits of information is called constraints. Only types this meet the constraints can be substituted for the given type parameter to produce constructed types.
3.5.1, Where clauses
Constraints is listed as WHERE clauses.
Each type parameter this has constraints have its own where clause.
If A parameter has multiple constraints, they is listed in the WHERE clause, separated by commas.
The syntax of a WHERE clause is the following:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/78/wKioL1ZoylThusRuAAAsqseCrPs596.png "title=" Qq20151209222300.png "alt=" Wkiol1zoylthusruaaasqsecrps596.png "/>
The important points about where clauses is the following: (multiple WHERE clause, note is plural form)
They ' re listed after the closing angle bracket of the type parameter list. (WHERE clause's location)
They ' re not separated by commas or any other token. (whether there is a delimiter)
They can listed in any order. (Order not important)
The token where is a contextual keyword, so you can use it on other contexts.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/77/79/wKiom1ZoylCCRKm3AABVE9-m0oI989.png "title=" Qq20151209222518.png "alt=" Wkiom1zoylccrkm3aabve9-m0oi989.png "/>
3.5.2, Constraint Types and Order
There is five types of constraints.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/77/78/wKioL1Zoyq-wyjG8AADu8JdbEZ4827.png "title=" Qq20151209222728.png "alt=" Wkiol1zoyq-wyjg8aadu8jdbez4827.png "/>
The WHERE clauses can is listed in any order. The constraints in a WHERE clause, however, must is placed in a particular order.
There can are at the most one primary constraint, and if there are one, it must be listed first.
There can is any number of interfacename constraints.
If The constructor constraint is present, it must was listed last.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/77/79/wKiom1Zoynmxu3zCAAAte_azYy0920.png "title=" Qq20151209223158.png "alt=" Wkiom1zoynmxu3zcaaate_azyy0920.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/77/78/wKioL1ZozZDznJ61AABcgUEirLU550.png "title=" Qq20151209223242.png "alt=" Wkiol1zozzdznj61aabcgueirlu550.png "/>
#读书笔记 #illustrated C # 2012 17th generics generics (1)