C #3.0 BASIC Language enhancements

Source: Internet
Author: User

Introduction to the Linq project and establishment of the Development Environment

In May September, Microsoft launched a new technology called the "Linq Project", which is used to integrate data query functions in the. NET language. You can get a technical preview of linqitem from http://msdn.microsoft.com/netframework/future/. it includes a large number of articles (in English), C #3.0, and VB 9.0 compilers.

Language Integrated Query is a Language Integrated Query ". Based on. NET Framework 2.0. Through language improvement, you can directly query the data source by constructing SQL-like statements in the language, data sources that can be queried can be extended from basic ordered structures (such as arrays and linked lists) to relational databases (currently SQL Server, and I believe it can be extended to almost all relational databases in the future) and XML.

C #3.0 is the version of the C # language after another upgrade. It is the first to implement the concept of Linq. VB 9.0 is also implemented in the same way. From the URL above, you can find the technical preview version of the compiler in two languages. In this article, we will focus on C #3.0 to discuss how to build the development environment and improve the language. Download an installation package named "linq preview. msi" (which may also be different). You can double-click it to install it like other software. This installation package provides plug-ins (project templates) and C # compilers for Visual Studio 2005 Beta 2 and later versions and Visual C #2005 Beta 2, the IL code generated by the compiler can be directly stored in.. NET Framework 2.0. After the Preview version is installed, under the Visual C # node in the project type list in the new project dialog box, we can see an entry of the Linq Preview, you can also select some desktop project templates from the project templates on the right (Web projects are not supported by Linq currently ).
As long as you select the project template in Linq, you can write the C #2005 Application in Visual Studio 3.0 just like writing other applications, during compilation, the IDE automatically selects the C #3.0 compiler for us.

Now we can start to write the C #3.0 application. In the subsequent sections, I will explain the language enhancements brought about by C #3.0. It is worth noting that this article is one of a series of articles, which consists of three parts. This article is the first part about C #3.0 BASIC Language enhancements. These language enhancements are the basis of the other two parts. The second part describes the Lambda expressions in C #3.0, this is a natural evolutionary form of anonymous methods. It can not only embody expressions as executable methods (delegates), but also represent expressions as data structures that can be operated at runtime-expression tree; the last part is about the most important and exciting content in the Linq project-the query expression, which is the implementation form of Linq in C. At the same time, the. NET Framework base class library is expanded for SQL queries and XML queries, respectively known as DLinq and XLinq. These contents will be described in other series of articles.

In the process described in this article, we only provide short code segments rather than complete code. However, these code segments are extracted from the complete, correctly compiled, and running code, which can be downloaded from here, the complete code is introduced in part 1 of this Article.

Declarations with implicit types

In a declaration statement with an initiator, the type of the variable to be declared is obvious-it is consistent with the result type of the initialization expression. In this case, the New Keyword var can be used in C #3.0 to replace the declared type, and the compiler can deduce the type of the Variable Based on the initialization expression.

Var I = 5; // int
Var d = 9.0; // double
Var s = "Hello"; // string
Var a = new int [] {1, 2, 3, 4, 5}; // int []

Console. WriteLine ("Type of variable: {0}", I. GetType ());
Console. WriteLine ("Type of variable: {0}", d. GetType ());
Console. WriteLine ("Type of variable: {0}", s. GetType ());
Console. WriteLine ("Type of variable: {0}", a. GetType ());

The above Code complies with the syntax rules in C #3.0. The first four lines of code use an implicit declaration; the last four lines of code are used to verify whether each variable has the correct type at runtime. If you run this code in Visual Studio 2005 (to see the results, use Ctrl + F5 to compile and start the program), you will get the following results:

Type of variable: System. Int32
Type of variable: System. Double
Type of variable: System. String
Type of variable: System. Int32 []

This indicates that the compiler has correctly deduced the type of each variable during compilation and embedded it into the Assembly metadata.

There are two restrictions: one is that declarations with implicit types can only be applied to local variables, and the other is that such declarations must have an initiatator (that is, the equal sign and the following expression ). If we attempt to declare a domain with an implicit type in a class, a compilation error occurs: Invalid token 'var' in class, struct, or interface member declaration; if the initialization tool does not appear in the declaration, another compilation error occurs: '=' expected.

In addition to local variables, an array with a block scope can also be declared with an implicit type. For example:

Var ia = new [] {1, 2, 3, 4, 5}; // int []
Var da = new [] {1.1, 2, 3, 4, 5}; // double []
Var sa = new [] {"Hello", "World"}; // string []

Console. WriteLine ("Type of array: {0}", ia. GetType ());
Console. WriteLine ("Type of array: {0}", da. GetType ());
Console. WriteLine ("Type of array: {0}", sa. GetType ());

In the array declaration in the preceding example, the type name is omitted between the new operator and a pair of square brackets indicating the array declaration, but this still complies with the syntax rules in C #3.0. The compiler will infer the type of the array by the value type in the member list. Compile and run the preceding example to get the following output:

Type of array: System. Int32 []
Type of array: System. Double []
Type of array: System. String []

In addition to the same constraints as local variables with implicit types, arrays with implicit types must also be exclusive to the rule that all values in the member list must be compatible. That is to say, such a value must exist in the member list so that other values can be implicitly converted to the type of the value. Therefore, the following statement does not comply with the syntax rules:

Var aa = new [] {1, "Hello", 2.0, "World "};

If you try to compile the above Code, you will get a compilation error: No array type can be inferred from the initializers. This is because the compiler cannot deduce the array type based on the values in the member list.

In fact, although implicit declarations facilitate the compilation of traditional declarations, the true purpose of introducing such declarations is not that, in order to allow local variables and arrays to access such a new language structure: anonymous type.

There are two pages in this news. Currently, there are two pages in page 1st.


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.