c#2.0 Introduction

Source: Internet
Author: User

C # 2.0 introduces many language extensions, most importantly generics (generics), anonymous methods (Anonymous Methods), iterators (iterators), and incomplete types (Partial Types).
●Generics allow classes, structs, interfaces, delegates, and methods to be parameterized by the type of data they are stored and manipulated. Generics are useful because they provide more powerful compile-time type checking, require fewer explicit conversions between data types, and reduce the need for boxing operations and type checking at run time.
●Anonymous methods allow you to write blocks of code in an "inline (in-line)" Way when you need a delegate value. The anonymous method is similar to the lambda function (lambda functions) in the Lisp language.
●Iterators are capable of incrementally calculating and generating a series of worthwhile methods. Iterators make it easy for a class to explain how a foreach statement will iterate over each of his elements.
●Incomplete types allow classes, structs, and interfaces to be divided into small chunks and stored in different source files to make them easy to develop and maintain. In addition, incomplete types can separate machine-generated code from user-written parts, making it easy to use tools to enforce the resulting code.
This chapter begins with a brief introduction to these new features. The introduction is followed by four chapters that provide complete technical specifications for these features.
The language extensions in C # 2.0 are designed to guarantee a high degree of compatibility with existing code. For example, although c#2.0 gives special meaning to the word where, yield, and partial in a particular environment, these words can be used as identifiers. Indeed, C # 2.0 does not add a keyword that will conflict with identifiers in existing code.

1.1 Generic type
Generics allow classes, structs, interfaces, delegates, and methods to be parameterized by the type of data they are stored and manipulated. C # Generics are quite cordial to users who use the Eiffel or ADA language generics and to users who use C + + templates, although they may not tolerate the complexity of the latter.

1.1.1 Why generics?
Without generics, some common data structures can only use object types to store various types of information. For example, the following simple stack class holds its data in an object array, and its two methods, push and pop, use object to accept and return data, respectively:
public class Stack
{
object[] items;
int count;
public void Push (item) {...}
public Object Pop () {...}
}
Although using the object type makes the stack class flexible, it is not without drawbacks. For example, you can push any type of value into the stack, such as a customer instance. However, retrieving a worthwhile time, you must explicitly convert the values returned by the Pop method to the appropriate type, writing these transformation changes to beware of run-time type checking errors is tedious:
Stack stack = new stack ();
Stack. Push (New Customer ());
Customer C = (customer) stack. Pop ();
If the value of a value type, such as int, is passed to the push method, it is automatically boxed. And when you retrieve this int value, you must have an explicit type conversion for the unboxing:
Stack stack = new stack ();
Stack. Push (3);
int i = (int) stack. Pop ();
&n</

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.