C #-based generic programming

Source: Internet
Author: User

1. Introduction
The concept of generic is not very new. the Templates of C has been widely used many years ago and has been widely used. The generic function is similar to the C template. It can define a type-safe data structure instead of the actual data type. Generic Programming (Generic Programming) is derived from the support of templates in C ++. STL (Standard Template Library, Standard Template Library) is one of the best implementations of Generic Programming. With the development of generic theory in recent years, generic programming technology has gradually become a general technology, and other languages are gradually supporting generic programming, in version 2.0 C # And CLR (Commom Language Runtime), generic support is achieved. This article mainly discusses the general problems used by generics in C # And understands generic applications, so as to help develop simple and efficient C # applications.
2. Concept of generics
The so-called generic type means that multiple data types can be operated on the same code using parameterized types. The first advantage of generics is strict type check during compilation. In addition, generics eliminate the vast majority of type conversions. We must have encountered this problem during programming: The implementation of multiple functions is almost the same, that is, only some parameters are different, but we cannot combine them into one, only repeated codes can be written separately. Generic programming can solve these problems perfectly. It can break the barriers between different data structures and simplify bloated program code. Generic programming is a programming paradigm. It abstracts types by using parameterized types to achieve more flexible reuse.
3. Program generics
The design philosophy of generic programs reflects the relationship between commonalities and characteristics, the goal is to remove common knowledge such as algorithms and data structures from specific application features without affecting the running efficiency .. Generic programs have many advantages: (1) general: Generic programs are the abstraction of a common problem. algorithms, data structures, and data types are mutually independent and can be used more widely. (2) Security: Generic programs define a type-safe data structure and do not need to use specific data types to avoid system disasters caused by type faults. (3) Reliability: the correctness of generic program commonality can ensure the reliability of all special cases. (4) Efficiency: the universality of generic programs is not at the cost of efficiency. It can ensure the program running efficiency is the same as that of non-generic programs.
4. Generic programming in C #
Generics introduce the concept of type parameters. NET Framework, type parameters make it possible to design the following classes and Methods: these classes and Methods delay one or more types until the client Code declares and instantiate the class or method. For example, by using the generic type parameter T, you can write a single class that can be used by other client code without introducing the cost or risk of force conversion or packing during runtime. Specify the actual type during class instantiation, and automatically compile the Runtime code as local code. The running efficiency and code quality are greatly improved, and data type security is ensured.
Create a generic class. The more types can be parameterized, the code can be streamlined, flexible, and reusable as much as possible. However, more types of generalization may make the entire program more obscure, other programmers may spend more time and energy to understand your program. The use of constraints is also necessary, and you need to have as many constraints as possible on generic applications, but it still enables you to process the types to be processed. If the programmer uses the wrong parameter type, the program can still be compiled normally. An error is reported only when this code is run, which puts your program in danger. The general process of creating a generic class can be summarized as: Starting from an existing specific class, changing each type to a type parameter one by one, coupled with the necessary constraints, until the optimal balance between general purpose and availability is achieved.
When writing generic classes, we may need to restrict the type types used for type parameter T during class instantiation. Here, we need to use the constraints of generic classes so that client code will generate a compilation error when trying to instantiate a class using a type not allowed by a constraint. C # specify the constraint by using the where keyword.
Generic usage is similar to usage in class methods. It can be used in parameterized methods. Let's look at the following code:
Public class Tlist {
Public T [] list;
Public int I;
Public Tlist (){
List = new T [10];
I = 0;
}
Public T Objout (){
Return list [-- I];
}
Public void Objin (T item) {// method 1
List [I ++] = item;
}
}
Public class Tlist_s {
Public void Objin (Tlist s, params T [] p) // method 2
{
Foreach (T t in p)
S. Objin (t );
}
}
Tlist T1 = new Tlist ();
Tlist_s T2 = new Tlist_s ();
T2.Objin (T1, 2, 3, 4, 5); // call 1
The method puts a single T-type item variable into the list array, while the method 2 puts multiple T-type variables into the list array. In the above example, the call is to put int type 2, 3, four or five elements are put into the list array. method 2 is the generic method.
5. Summary
Compared with the generics of other languages, C # has its advantages in binary reusability. The generic type of C ++ relies on the features provided by its compiler. When compiling C ++ generic code, the actual type parameter is replaced, this may cause code expansion. In C #, generic code is compiled In real Time by JIT (Just-In-Time, compiler that compiles MSIL code into local code dedicated to the operating system and target machine, CRL (Commom Language Runtime, Common Language Runtime Library) can reuse most of the real-time compiled code of different types.
Contribution
[1] Anders Hejlsberg, Scott Wiltamuth. C # Programming Language [M]. New York: Addison Wesley, 2003.
[2] Sun Bin. object-oriented and generic programming design and type constraints check [J]. Journal of computer science, 2004
[3] Xue Jinyun. Programming method [M]. Beijing: Higher Education Press, 2005.
[4] lippman s B. lajoie j. moo B E C ++ primer, 2006
[5] Austern m h. Generic programming and STL [M]. Essex: Addison2Wesley Longmax, 1999.
[6] John Sharp. Visual C #2005 from entry to master. Tsinghua University Press, 2006
[7] Liu Hongcheng. C # advanced programming. Tsinghua University Press. 2003

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.