Visual C # 2005 Preview

xiaoxiao2021-03-06  38

Introduction

The next version of Microsoft Visual Studio significantly enhances the functionality of C #, including innovative language structure, new compiler function, greatly enhances the productivity of developers, strengthening adjustment function, and so on. In terms of program language innovation, the C # Whidbey release supports generics, Iterator, partial types, and anonymous methods. Whidbey's new compiler feature allows developers to disable compiler warnings directly in the code, or verify ECMA / ISO consistency. Whidbey will also have several productivity enhancements, including refactoring, code expansion, code formatting, enhanced IntelliSense. The debugging function also has strengthened, new features include enhanced DataTips, debugging visual tools, design-based expression time assessments, etc. This article only sumbsects new features in WHIDBEY, we will continue to join the new features needed to customers.

Language innovation function

Generic

The generic is one of the most important new features of the C # language in Whidbey. C # generic can allow class, structure, interface, and method, parameterized according to its storage and processing data type. Wild is used, because many common categories and structures can be parameterized by their storage and processing data types. These are called "generic declarations" and "generic structural declarations". Similarly, many interface define contracts, which can be parameterized according to the data type processed. These are called "generic interface declarations". The method can also be parametric according to its type to achieve the "generic algorithm", which is called "generic method."

In the following example, we created a Stack generic class declaration, which specifies the type parameters, called ItemType, declares in a declaration after the declaration. The generic STACK class instance does not force the object between the object to be converted back, but accept the type of creation, and store the type of data when converted. The role of the ItemType type parameter is as placeholder until the actual type is specified. Note that ItemType is used for element types of internal project arrays, and the parameter type of the PUSH method, and the return type of the POP method:

Public class stack {private itemtype [] items; public void push (itemtype data) {...} public itemtype pop () {...}}

As shown in the following short example, when using the STACK generic class declaration, the actual type used in the generic class can be specified. In this case, we require Stack to use int type by specifying "Type Ginsen" within the number of angle brackets after the name.

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

In this way, we create a new "constructed type" stack , where the Stack declares that each ItemType inside the inside will be replaced with the Int type of INT. In fact, when establishing a new instance of Stack , for storage efficiency considerations, the original storage type of the project array is now int [], not Object []. In addition, when INT is pushing to the stack, it is no longer necessary to convert it. Even when the project is removed from the stack, it is no longer necessary to convert it to the appropriate type, because the special class of Stack is stored in its data structure, stored in its original type.

To store non-int items to STACK, you must establish a different constructor by Stack and specify a new type of ginseng. If there is a simple Customer class, we want to use Stack to store. To achieve this, just use the Customer class as a type of Stack, you can easily repeat the code: Stack Stack = New Stack (); stack.push (new customer ()); Customer C = stack.pop ();

Of course, once a Stack is established as a type of Customer type, only the Customer object (or object being derived from the Customer class) can be stored. The generic provides "strong type", that is, we can't store the integer in the stack:

Stack stack = new stack (); stack.push (new customer ()); stack.push (3); // compile-time errorcustomer c = stack.pop (); // no cast required

Part type

Site types allow single types, such as a class, dispersed into multiple files. This feature is most useful to Visual Studio and other code builders, which can store the generated code to code written in different files with the code written by the usage. By this manner, the design tool can easily analyze and reset the code without affecting the code written by the user. E.g:

// Form1Designer.cspublic partial class Form1: System.Windows.Forms.Form {// design code void InitializeComponent () {...}} // Form1User.cspublic partial class Form1 {// user code void Mouse_Click (object sender , MouseEventArgs E) {...}}