C #2.0 Introduction

Source: Internet
Author: User

C #2.0 introduces many language extensions. The most important ones are generic, Anonymous Methods, Iterators, and Partial Types ).
● Generics allow parameterization of classes, structures, interfaces, delegates, and methods through the data types they store and operate on. Generics are useful because they provide more powerful type checks during compilation and require Explicit conversions between fewer data types, in addition, it reduces the need for packing and runtime type checks.
● The anonymous method allows you to write code blocks in "in-line" mode when you need to delegate values. The anonymous method is similar to lambda functions in Lisp.
● The iterator can incrementally calculate and generate a series of worthwhile methods. The iterator enables a class to easily explain how the foreach statement iterates each of its elements.
● Incomplete types allow classes, structures, and interfaces to be divided into multiple small blocks and stored in different source files for easy development and maintenance. In addition, incomplete types can be used to separate the code generated by the machine and the part written by the user, which makes it easy to use tools to enhance the generated code.
This chapter first introduces these new features. The introduction is followed by four chapters that provide complete technical specifications for these features.
The Language extension design in C #2.0 ensures high compatibility with existing code. For example, although C #2.0 assigns special meaning to the word where, yield, and partial in a specific environment, these words can still be used as identifiers. Indeed, C #2.0 does not add a keyword that will conflict with the identifier in the existing code.

1.1 generic
Generics allow classes, structures, interfaces, delegates, and methods to be parameterized by the data types they store and operate on. C # generics are very friendly to users who use extensions in the Eifel Or Ada Language and users who use the C ++ template, although they may not be able to tolerate the complexity of the latter.

1.1.1 why is it generic?
No generics. Some common data structures can only store various types of data using the object type. For example, the following simple Stack class stores its data in an object array, and its two methods, Push and Pop, use the object to accept and return data respectively:
Public class Stack
{
Object [] items;
Int count;
Public void Push (object item ){...}
Public object Pop (){...}
}
Although the object type is used to make the Stack class very flexible, it is not a disadvantage. For example, you can press any type of value to the stack, such as a Customer instance. However, when retrieving a value again, you must explicitly convert the value returned by the Pop method to an appropriate type. Writing these conversion changes is tedious to guard against runtime type check errors:
Stack stack = new Stack ();
Stack. Push (new Customer ());
Customer c = (Customer) stack. Pop ();
If a value of the value type, such as int, is passed to the Push method, it is automatically packed. When you retrieve the int value later, you must perform explicit type conversion to unpack it:
Stack stack = new Stack ();
Stack. Push (3 );
Int I = (int) stack. Pop ();
& N

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.