C # Generic lectures

Source: Internet
Author: User

Video address: http://www.56.com/u88/v_MjIwODE1NjU.html processor address: http://www.cnblogs.com/hjf1223/archive/2005/08/25/222970.html

The lecture content is organized by instructor Li Jianzhong.
The so-called generic type means that multiple data types can be operated on the same code by using parameterized types. Generic programming is a programming paradigm that abstracts types by using parameterized types, this allows for more flexible reuse.

C # generics give the code stronger type security, better reuse, higher efficiency, and clearer constraints.

Mechanism:
C # The generic capability is supported by CLR at runtime. It is different from the C ++ compile-time template mechanism and the Java compile-time "erase" method ". This enables seamless interoperability between languages that support CLR.
C # When the generic code is compiled into the Il code and has no data, special Placeholders are used to represent the generic type, and proprietary il commands are used to support generic operations. The real generic instantiation work is in the "On-Demand" mode, which occurs during JIT compilation.

C # Generic compilation mechanism:
During the first round of compilation, the compiler only generates the "generic version" il code and metadata for the stack <t> (Stack Algorithm) type and does not instantiate the generic type, t acts as a placeholder in the middle
During JIT compilation, when the JIT compiler encounters a stack <int> for the first time, it replaces the "generic version" il code with the T-in the metadata to instantiate the generic type.
CLR generates the same code for all generic types whose type parameters are "reference type". However, if the type parameter is "Value Type", for each different "Value Type ", CLR generates an independent code for it.

C # several features of generics:
If the parameters of the instantiated generic type are the same, the JIT compiler will reuse this type, therefore, the dynamic generic capability of C # avoids code expansion problems caused by C ++ static templates.

C # generic types carry rich metadata, So C # generic types can be applied to powerful reflection technologies.

The generic type of C # adopts the constraints of "base class, interface, constructor, value type/reference type" to implement the "Explicit constraint" on the number of data types ", this improves the type security, and also loses the high flexibility of the C ++ template based on the "signature" implicit constraint.

Generic Type members:

A member of a generic type can use a type parameter in a generic type declaration. If there are no constraints on a type parameter, only public members inherited from system. object can be used for this type.

Generic interface:
The type parameters of a generic interface are either instantiated or derived from the type parameters declared by the Implementation class.

Generic delegation:
Delegate bool predicate <t> (T value );
Class X {
Static bool F (int I ){..}
Static bool g (string s ){..}
Static void main ()
{
Predicate <string> P2 = g;
Predicate <int> P1 = new predicate <int> (f );
}
}
Generic delegation supports applying parameter types to delegate return values and parameters. These parameter types can also be subject to legal constraints.

Generic Method Introduction:
C # The generic mechanism only supports "Adding type parameters to method declarations"-that is, generic methods
C # The generic mechanism does not support the inclusion of type parameters in the declaration of other members (including attributes, events, indexers, constructors, and Destructors) except the method, however, these members can be included in the generic type and use the generic type parameters.
Generic methods can be included in both generic and non-generic types.
Public class finder {
Public static int find <t> (T [] items, t item ){
For (INT I = 0; I <items. length; I ++)
{
If (...) reutrn I;
}
Return-1
}
}
// Generic method call
Int I = finder. Find <int> (New int [] {1, 2, 3, 4, 5}, 6 );

Generic constraints:
C # any assumption of "all generic type or generic method type parameters" must be based on "Explicit Constraints ", to maintain the type security required by C.
Explicit Constraints are expressed by the WHERE clause. You can specify "base class constraints", "interface constraints", and "constructor constraints ", the value type/reference type constraint has four constraints.
Explicit Constraints are not mandatory. If explicit constraints are not specified, generic parameters can only access public methods in the system. Object type.
The following are four different constraints:


 

Video address: http://www.56.com/u88/v_MjIwODE1NjU.html processor address: http://www.cnblogs.com/hjf1223/archive/2005/08/25/222970.html

The lecture content is organized by instructor Li Jianzhong.
The so-called generic type means that multiple data types can be operated on the same code by using parameterized types. Generic programming is a programming paradigm that abstracts types by using parameterized types, this allows for more flexible reuse.

C # generics give the code stronger type security, better reuse, higher efficiency, and clearer constraints.

Mechanism:
C # The generic capability is supported by CLR at runtime. It is different from the C ++ compile-time template mechanism and the Java compile-time "erase" method ". This enables seamless interoperability between languages that support CLR.
C # When the generic code is compiled into the Il code and has no data, special Placeholders are used to represent the generic type, and proprietary il commands are used to support generic operations. The real generic instantiation work is in the "On-Demand" mode, which occurs during JIT compilation.

C # Generic compilation mechanism:
During the first round of compilation, the compiler only generates the "generic version" il code and metadata for the stack <t> (Stack Algorithm) type and does not instantiate the generic type, t acts as a placeholder in the middle
During JIT compilation, when the JIT compiler encounters a stack <int> for the first time, it replaces the "generic version" il code with the T-in the metadata to instantiate the generic type.
CLR generates the same code for all generic types whose type parameters are "reference type". However, if the type parameter is "Value Type", for each different "Value Type ", CLR generates an independent code for it.

C # several features of generics:
If the parameters of the instantiated generic type are the same, the JIT compiler will reuse this type, therefore, the dynamic generic capability of C # avoids code expansion problems caused by C ++ static templates.

C # generic types carry rich metadata, So C # generic types can be applied to powerful reflection technologies.

The generic type of C # adopts the constraints of "base class, interface, constructor, value type/reference type" to implement the "Explicit constraint" on the number of data types ", this improves the type security, and also loses the high flexibility of the C ++ template based on the "signature" implicit constraint.

Generic Type members:

A member of a generic type can use a type parameter in a generic type declaration. If there are no constraints on a type parameter, only public members inherited from system. object can be used for this type.

Generic interface:
The type parameters of a generic interface are either instantiated or derived from the type parameters declared by the Implementation class.

Generic delegation:
Delegate bool predicate <t> (T value );
Class X {
Static bool F (int I ){..}
Static bool g (string s ){..}
Static void main ()
{
Predicate <string> P2 = g;
Predicate <int> P1 = new predicate <int> (f );
}
}
Generic delegation supports applying parameter types to delegate return values and parameters. These parameter types can also be subject to legal constraints.

Generic Method Introduction:
C # The generic mechanism only supports "Adding type parameters to method declarations"-that is, generic methods
C # The generic mechanism does not support the inclusion of type parameters in the declaration of other members (including attributes, events, indexers, constructors, and Destructors) except the method, however, these members can be included in the generic type and use the generic type parameters.
Generic methods can be included in both generic and non-generic types.
Public class finder {
Public static int find <t> (T [] items, t item ){
For (INT I = 0; I <items. length; I ++)
{
If (...) reutrn I;
}
Return-1
}
}
// Generic method call
Int I = finder. Find <int> (New int [] {1, 2, 3, 4, 5}, 6 );

Generic constraints:
C # any assumption of "all generic type or generic method type parameters" must be based on "Explicit Constraints ", to maintain the type security required by C.
Explicit Constraints are expressed by the WHERE clause. You can specify "base class constraints", "interface constraints", and "constructor constraints ", the value type/reference type constraint has four constraints.
Explicit Constraints are not mandatory. If explicit constraints are not specified, generic parameters can only access public methods in the system. Object type.
The following are four different constraints:


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.