C # v2.0 extension characteristics translation (1)

zhaozj2021-02-16  71

Introduction to C # 2.0

C # 2.0 Introduces Several Language Extensions, The Most Important Of Which Are Gnerics, Anonymous Methods, Iterators, and Partial Types.

C # 2.0 mainly introduces several language extensions, generics, anonymous methods, iterators, and Partial Types.

· Generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate. Generics are useful because they provide stronger compile-time type checking, require fewer explicit conversions between data types, and reduce the NEED for boxing operations and run-time Type Checks.

The generic allows class, structures, interfaces, proxy and methods are parameterized by data types they store and operate. The generic is quite useful because they offer stronger compile time type checks, requiring less explicit conversions between data types, and reduces the need for the installed operation and runtime type check.

· Anonymous methods allow code blocks to be written "in-line" where delegate values ​​are expected. Anonymous methods are similar to lambda functions in the Lisp programming language. C # 2.0 supports the creation of "closures" where anonymous methods access surrounding local variables and Parameters.

· Iterators Are Methods That Incrementally Compute and Yield A Sequence of Values. Iterators Make ItEas for a Type To Specify How The Foreach Statement Will Iterage over Its Elements.

· Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user-written parts of types so that it is easier to Augment code generated by a Tool.

This is the feature.

This chapter will introduce these new features. The subsequent four chapters will provide the full technical specification of the characteristics The Language Extension IN C # 2.0 WERE Designed to Ensure Maximum Compatibility with existing code. For Example, Even Though C # 2.0 Gives special meaning to the words where, yield, and partial in certain contexts, these words can still be used as identifiers. Indeed, C # 2.0 adds no new keywords as such keywords could conflict with identifiers in existing code.

The language extension in C # 2.0 is in design and ensuring the compatibility with existing code. For example, even if C # 2.0 specifies the following words, the partial has special meaning in a particular context, but they can still be used as a marker. Even, C # 2.0 does not add any new keywords that may conflict with existing code

19.1 Generics

GENERICS Permit Classes, Structs, Interfaces, Delegates, And Methods To Be Parameterized by The Types of Data Thei Store and Manipulate. C # generics will be immediely familiar to users of generics in Eiffelier

ADA

, or to users of C Templates, Though They Do Not Suffer Many of The Complications of The Latter.

(Pan type, structure, interface, proxy, and method are parameterized by their storage of data types). C # generics will soon be familiar with generic users in Eiffel, Ada, or users who use C Templates, although they don't endure future multi-compilers.

19.1.1 Why menerics?

Without generics, general purpose data structures can use type object to store data of any type. For example, the following simple Stack class stores its data in an object array, and its two methods, Push and Pop, use object to accept and return data Respectively:

If there is no generic, the general data structure can store any data type using the object type. For example, a simple stack stored in the following is in an object array. It has two methods, PUSH and POP, use objects to accept and return data separately.

Public Class Stack {Object [] Items; int Count;

Public void push (Object item) {...}

Public Object Pop () {...}}

While the use of type object makes the Stack class very flexible, it is not without drawbacks. For example, it is possible to push a value of any type, such a Customer instance, onto a stack. However, when a value is retrieved, The Result of the Pop Method Must Explicitly Be Cast Back to The Appropriate Type, Which IS TEDIOS TO WRITE AND Carries a Performance Penalty For Run-Time Type Checking: The use of object types is more flexible, but it is not perfect. For example, it is likely to be pressing any type of value, such as a Customer instance. However, when a value returns, the result returned by the POP method must explicitly transform into an appropriate type, not only written is bored and detected in the runtime check to reduce performance.

Stack stack = new stack (); stack.push (new customer ()); Customer C = (Customer) stack.pop ();

If a value of a value type, such as an int

If it is a value type, such as an integer incoming Push method, it automatically box. When the integer is later returned, it must make an explicit unpacking.

Stack stack = new stack (); stack.push (3); int i = (int) stack.pop ();

.

When they are in dynamic memory allocation and runtime type check, the packing unpacking operation will increase performance consumption.

A further issue with the Stack class is that it is not possible to enforce the kind of data placed on a stack Indeed, a Customer instance can be pushed on a stack and then accidentally cast it to the wrong type after it is retrieved.:

It is impossible to further discuss the STACK class, which is impossible to add data from the category to the stack. In fact, a Customer instance can be pressed into the stack and is likely to be transformed into an error when it returns.

Stack stack = new stack (); stack.push (new customer ()); string s = (String) stack.pop ();

While the code above is an improper use of the Stack class, the code is technically speaking correct and a compile-time error is not reported. The problem does not become apparent until the code is executed, at which point an InvalidCastException is thrown.The Stack Class Would Clearly Benefit from the Ability To Specify ITS Element Type. With generics, That Becomes Possible.

The above code is technically correct and it is incorrect when compiling, but the usage of the Stack class is incorrect. This problem is displayed until the code is executed and the InvalidCastException exception is thrown.

Stacks should have the ability to benefit from its designated element type. This will become possible after a generic.

Creating and using generics

Create and use generics

Generics provide a facility for creating types that have type parameters. The example below declares a generic Stack class with a type parameter T. The type parameter is specified in delimiters after the class name. Rather than forcing conversions to and from object, instances of Stack accept the type for which they are created and store data of that type without conversion. The type parameter T acts as a placeholder until an actual type is specified at use. Note that T is used as the element type for THE INTERNAL ITEMS ARRAY, The Type for the parameter to the push method, and the return type for the pop mathod:

The generics provide a convenient way to create types through type parameters. The following example declares a generic STACK class by the type parameter T. Type parameters are defined in the <> separator after class name. The Stack instance accepts the data type it creates and stored without the need to convert, this is much better than the mandatory object conversion. The type parameter T played a placeholder role until the real type was specified. Note that T is used as an element type of the intrinsic array, as the parameter of the PUSH and the return type of POP.

Public Class Stack {T [] Items; int Count;

Public void push (t item) {...}

PUBLIC T POP () {...}}

When the generic class stack is buying, the actual type to substruction for t is specified. In The Following Example, Int IS Given As The Type Argument for t:

When the generic class Stack is used, the real type of replacement T will be specified. In the example below, int is specified instead of T. Stack stack = new stack (); stack.push (3); int x = stack.pop ();

The Stack type is called a constructed type. In the Stack type, every occurrence of T is replaced with the type argument int. When an instance of Stack is created, the native storage of the items array is an int [] rather than object [], providing substantial storage efficiency compared to the non-generic Stack. Likewise, the Push and Pop methods of a Stack operate on int values, making it a compile-time error to push VALUES OF OTHER TYPES ONTO THE Stack, and Eliminating the need to expected type at the used ignittribute.

Stack type is called constructive type.. In Stack , T will be replaced by the type parameter INT each time. When a Stack instance is created, itself storage Items array is an INT type array, providing authenticity storage efficiency than an object array of non-float stacks. Similarly, use the PUSH and POP methods to operate the int value. If the Push other types of data gives the stack, it will result in a compile time error, which excludes the explicit conversion as required when the value is returned.

Generics provide strong typing, meaning for example that it is an error to push an int onto a stack of Customer objects. Just as a Stack is restricted to operate only on int values, so is Stack restricted to Customer objects And the Compiler Will Report Errors on the last two lines of the folload example:

The generics provide strong types, which means that the exemplification of the stack that presses a integer data into the Customer flue type will result in errors. Just as an INT generic stack is strictly constrained to operate the INT type, so the Customer generic stack is strictly required to operate the Customer object. Then the last two lines of the following example, the compiler will report an error.

Stack stack = new stack (); stack.push (new customer ()); Customer C = stack.pop (); stack.push (3); // type mismatch errorint x = stack.pop (); // Type mismatch Error

Generic type declarations may have any number of type parameters. The Stack example above has only one type parameter, but a generic Dictionary class might have two type parameters, one for the type of the keys and one for the type of the values : The generic statement can include any number of type parameters. The example above STACK has only one type parameter, but the generic Dictionary class can contain two types of parameters, one is the type of keyword, one is the type of value.

Public Class Dictionary {public void add (k Key, v value) {...}

Public v this [k key] {...}}

When Dictionary is buy, two type arguments would have to be supplip:

Dictionary When using, two types of parameters must be provided

Dictionary DICT = New Dictionary (); DICT.ADD ("peter", new customer ()); Customer C = DICT ["peter"];

转载请注明原文地址:https://www.9cbs.com/read-15228.html

New Post(0)