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
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
Public Class Stack
Public void push (t item) {...}
PUBLIC T POP () {...}}
When the generic class stack
When the generic class Stack
The Stack
Stack
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
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
Generic type declarations may have any number of type parameters. The Stack
Public Class Dictionary
Public v this [k key] {...}}
When Dictionary
Dictionary
Dictionary