Future features of the C # programming language

Source: Internet
Author: User
Tags anonymous definition class definition functions implement new features microsoft c visual studio
Future functions of programming C # programming language

Prashant Sridharan
Microsoft Corporation
March 2003
Apply to:
Microsoft®visual C # (TM)

Summary: Microsoft Corporation is developing the next major version of the C # language. This article describes four major new features: Generics, iterators, anonymous methods, and local types.

Directory
Brief introduction
Generic type
Iterative Program
anonymous method
Local type
Conform to the standard
Availability of
More information
Brief introduction
C # is an innovative new programming language that skillfully combines the most commonly used industry language and research language functions. While keeping the C # design in mind, Microsoft has introduced several potential new features in the C # language, increasing the developer's efficiency in language construction.

Microsoft C #
Since the advent of C # in February 2001, many developers have begun to use the C # programming language to build software. Microsoft itself also uses C # to build several formal applications, including the. NET Framework, the MSN Web properties, and the Tablet PC SDK. Thus, C # is a language suitable for constructing high quality commercial software.

Many of the features in the C # language are created based on the following four different design goals:

A unified type system and the use of simplified value types and reference types in the C # language.
Build component-based design through the functions of XML annotations, attributes, attributes, events, and delegates.
The use of the unique features of the C # language (including safe pointer operations, overflow checking, etc.) to establish a practical developer control function.
Build practical language constructs such as foreach and using statements to improve the efficiency of developers.
In the "Visual Studio for Yukon" version of the C # language, Microsoft plans to build a concise, practical syntax by combining a wide range of research languages with various features in the industry language. These language features include generics, iterators, anonymous methods, and local types.

Potential future capabilities
In fact, the future innovation function of C # is mainly based on the unified type System, component-based development, developer control function and practical language construction. The following is a summary of the four major new features that Microsoft plans to provide in the next major version of the C # language. The design of these features has not yet been completed, and Microsoft Corporation welcomes extensive developers to comment on these features.

Generic type
As projects become more complex, programmers increasingly need a way to better reuse and customize their existing component-based software. In order to implement the reuse of advanced code in other languages, programmers typically use a feature called generics. C # will include a safe and efficient generic that differs slightly in syntax from the generics proposed in the templates in C + + and the Java language, but there are significant differences in how they are implemented.

Generate the latest generic class
With current C #, programmers can create a finite form of true generics by storing data in an instance of the base object type. Because each object in C # is inherited from the base object type, plus the boxing and unboxing capabilities of the unified. NET type system, programmers can store reference types and value types in variables of object types. However, there are some performance flaws in the conversion between reference types, value types, and base object types.

To illustrate this, the following code example creates a simple Stack type that contains two actions "Push" and "Pop". The Stack class stores its data in an array of object types, and the Push and Pop methods use the Base object type to accept and return data:

public class Stack
{
Private object[] items = new OBJECT[100];

public void Push (object data)
{
...
}

public Object Pop ()
{
...
}
}
You can then press the custom type, such as the Customer type, onto the stack. However, if your program needs to retrieve data, you need to explicitly convert the results of the Pop method (the Base object type) to the Customer type.

Stack s = new stack ();
S.push (New Customer ());
Customer C = (customer) s.pop ();
If a value type, such as an integer, is passed to the Push method, the runtime automatically converts it to a reference type, which is called boxing, and then stores it in an internal data structure. Similarly, if a program wants to retrieve a value type from the stack, such as an integer, the object type obtained from the Pop method needs to be explicitly converted to a value type, which is called unboxing:

Stack s = new stack ();
S.push (3);
int i = (int) s.pop ();
Boxing and unboxing operations between value types and reference types are onerous.

Also, in the current implementation, the data types placed in the stack cannot be restricted. In fact, you can create a stack and then press the Customer type onto the stack. You can then use the same stack and try to pop the data, and then convert it to another type, as shown in the following example:

Stack s = new stack ();
S.push (New Customer ());

Employee E = (employee) s.pop ();
Although the previous code example mistakenly used a single type of Stack class to implement, it should be treated as an error, but it is actually a legitimate code and there is no problem compiling it. At run time, however, the program will fail due to an invalid conversion operation.

Creating and using Generics
Generics in C # can create efficient data structures that are intended for use by the compiler based on the type they are using. After these so-called parameterized types are created, their internal algorithms remain unchanged, but the types of their internal data can be changed with the end user settings.

To help developers save time learning the language, the generic declaration methods in C # are roughly the same as in C + +. Programmers can create classes and structs in the usual way and specify type parameters using angle bracket tags (< and >). When you use a class, you must replace each parameter with the actual type provided by the user of the class.

The following example creates a Stack class that specifies and declares a type parameter named ItemType in the angle brackets after the class declaration. An instance of a generic Stack class will accept the type created for it and store data for that type locally, rather than converting between the created type and the base object type. The type parameter ItemType acts as a proxy until the type is specified in the instantiation process and used as the type of the internal item array (that is, the argument type of the Push method and the return type of the Pop method):

public class Stack<itemtype>
{
Private itemtype[] items;

public void Push (ItemType data)
{
...
}

Public ItemType Pop ()
{
...
}
}
When a program uses the Stack class in the following example, you can specify the actual type that the generic class uses. This example uses the angle bracket notation in the instantiation statement to specify the original integer type as a parameter, indicating that the Stack class uses this type:

stack<int> stack = new stack<int> ();
Stack. Push (3);
int x = stack. Pop ();
When you do this, the program creates a new instance of the Stack class, where each ItemType is replaced by the supplied integer parameter. In fact, when a program creates a new instance of the Stack class with an integer argument, the project array that is stored locally inside the Stack class is an integer, not an object. The program also eliminates the boxing problem associated with putting integers on the stack. In addition, when a program pops up an item from the stack, you do not have to explicitly convert it to the appropriate type, because the current specific instance of the Stack class stores the integer locally in its data structure.

If you want your program to store other types of items in the Stack class, you must create a new instance of the Stack class and specify the new type as a parameter. Suppose you have a simple Customer type, and you want your program to store the type using a Stack object. To do this, you can easily reuse program code by simply instantiating the Stack class and using the Customer object as its type parameter:

stack<customer> stack = new stack<customer> ();
Stack. Push (New Customer ());
Customer C = Stack. Pop ();
Of course, if your program creates a stack class that takes the customer type as a parameter, you can store the customer type only on that stack. In fact, generics in C # have strict types, which means that you cannot store integers in the stack, as shown in the following example:

stack<customer> stack = new stack<customer> ();
Stack. Push (New Customer ());
Stack. Push (3)//compile-time error
Customer C = Stack.      Pop (); Type conversions are not required.
Advantages of generics
With generics, programmers can reuse the code for a variety of different data types by writing, testing, and deploying the code once. The first stack example has this feature, and the second stack example allows a program to reuse code that has little impact on its application performance. For value types, the first stack example has a large performance problem, and the second stack example completely eliminates the problem because it removes boxing and downward type conversions.

Also, generics are checked at compile-time. When a program instantiates a generic class with a supplied type parameter, this type parameter can only be the type that the program specifies in the class definition. For example, if a program creates a stack of a Customer object type, the integer cannot be pressed onto the stack. By enforcing this action, you can generate more reliable code.

In addition, the C # Implementation of generics reduces the speed of code expansion compared to other stringent type implementations. Creating a collection of types with generics allows you to avoid creating specific variants of each class while maintaining the operational performance advantage. For example, a program can create a parameterized Stack class without creating integerstack for storing integers, stringstack for storing strings, and customerstack for storing Customer types.

This can increase the readability of your code. By simply creating a stack class, the program can encapsulate all operations associated with a stack in a convenient class. Then, when creating the customer type stack, although the customer type is stored, it is obvious that the program is still using the stack data structure.

Multiple type parameters
Generics can use any number of parameter types. Only one type is used in the Stack example above. Suppose you create a simple Dictionary class that stores values and keys. A generic version of the Dictionary class can be defined in a program by declaring two arguments (placed in the angle brackets of the class definition and separated by commas):

public class Dictionary<keytype, valtype>
{
public void Add (KeyType key, Valtype val)
{
...
}

Public Valtype This[keytype Key]
{
...
}
}
When using the Dictionary class, you need to provide multiple comma-delimited arguments in the angle brackets of the instantiated statement and provide the correct type of parameters for the ADD function and the index Builder:

Dictionary<int, customer> dict = new Dictionary<int, customer> ();
Dict. ADD (3, New Customer ());
Customer C = Dict. GET[3];
Constraints
Typically, a program is not limited to storing data based on a given type parameter, but often requires members of the type parameter to execute statements in the program's generics.

Why do I need constraints

Suppose in the Add method of the Dictionary class, you need to compare the project using the CompareTo method of the provided key, for example:

public class Dictionary<keytype, valtype>
{
public void Add (KeyType key, Valtype val)
{
...
Switch (Key.compareto (x))
{
}
...
}
}
Unfortunately, as expected, type parameter KeyType is generic at compile time. If you write this, the compiler assumes that a key instance that is KeyType to a type parameter can only perform operations that apply to the Base object type (for example, ToString). As a result, the compiler will display a compilation error because the CompareTo method is undefined. However, a program can convert a key variable to an object that contains a CompareTo method, such as a IComparable interface. In the following example, the program explicitly converts an instance key of the KeyType parameter type to a IComparable interface that the program can compile:

public class Dictionary<keytype, valtype>
{
public void Add (KeyType key, Valtype val)
{
...
Switch ((IComparable) key). CompareTo (x))
{
}
...
}
}
However, if the Dictionary class is immediately instantiated and the supplied type parameter does not implement the IComparable interface, the program encounters a run-time error, especially a InvalidCastException exception.


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.