Atitit. Generic Programming summary Best Practices vO99 java C + + c#.net PHP

Source: Internet
Author: User

Atitit. generic Programming Summary Best Practices vO99 java C + + c#.net PHP

\

1. Generic History 1

Origin 1

2. Generic mechanism editing 1

mechanism 1

compilation mechanism 2

3. Generic method definition 1:: pre-defined 2

4. Generic method definition 2:: after definition , methods also need to add <T> tags ... 2

5. Call the generic method ::: Cls.<string>meth (); 2

6.// return value to model 2

7. Reference 3

1. Generic History

The motivation for generic programming (Generic programming) was simple and straightforward: inventing a language mechanism that can help implement a common standard container library. The so-called Universal Standard Container Library, is to be able to do, such as the use of a List class to store all possible types of objects such things; generic programming allows you to write fully generalized and reusable algorithms that are efficient compared to a specific Data types and the same algorithms are designed. Generics are meant to be manipulated on a variety of data types , somewhat similar to templates. STL huge and extensible, it contains many basic computer algorithms and data structures, and completely separates the algorithm from the data structure, where the algorithm is generic and not tied to any particular data structure or object type.

Origin

Generic programming was originally born in C + + and was founded by Alexander stepanov[2] and David musser[3 . The purpose is to implement C + + STL( Standard Template Library). The language support mechanism is the template (Templates). The spirit of the template is actually very simple: parameterized type. In other words, the type information in an algorithm or class that is originally specific to a type is pumped out and made into a template parameter T. For example , after qsort generalization, it becomes:

Author :: old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2.the mechanism of a generic typeEditmechanism

C # Generic code uses special placeholders to represent generic types when it is compiled into IL Code and no data, and Generic operations are supported with proprietary IL directives. And the true generics instantiation works in a "On-demand" Way, at JIT compile time.

compilation mechanism

1.  First round compilation, compiler stack<t > ( " generic version " il code and meta-data ----- does not instantiate a generic type, t In the middle only acts as

2.JIT Compilation When the JIT compiler encounters stack<int> for the first time , it replaces the " Generic version "IL code and metadata in the T--- the instantiation of a generic type. the CLR for all type parameters is " Reference type" produces the same code as the generic type, but if the type parameter is " value type", for each of the different value types ,theCLR generates a separate piece of code for it.

3. Generic method definition 1:: Pre-defined


public static <atiType,retType> RetType reduceO4 (atitype obj, Func function) {

4. Generic method definition 2:: After definition, methods also need to add <T> tags ...

public void bindlist2grid<t> (list<t> li, DataTable dt)
{

5. Call the generic method::: Cls.<string>meth (); 6. Inverse return value is paradigm

public static T getvalue<t> (object entity, String fieldName)

7. Reference

Paip. the type or namespace name could not be found when using generics " T-attilax 's Column -  Blog Channel -CSDN.NET.htm

Paip. Custom Java Summary of implementation of generic classes and generic methods -Attilax 's Column - .

Atitit. Generic Programming summary Best Practices vO99 java C + + c#.net PHP

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.