Basic Language enhancements in C # 3.0 language explanation

Source: Internet
Author: User

Introduction to LINQ project and building of development environment

In September, Microsoft launched a new technology called "LINQ Project" for the. NET language, the ability to integrate data queries. You can get a technical preview of the LINQ project from Http://msdn.microsoft.com/netframework/future/, which includes a large number of introductory articles (English) and compilers for C # 3.0 and VB 9.0.

LINQ Project English is all called Language Integrated query, that is, language integration queries. Based on the. NET Framework 2.0, LINQ is used to query the data source directly in the language through the construction of SQL statements, and the queried data source extends from the basic sequential structure (such as arrays and lists) to the relational database (currently SQL Server, Believe that you can extend to almost all relational databases in the future and XML.

C # 3.0 is the second version of the C # language, the first to implement the concept of LINQ, as well as the implementation of LINQ and VB 9.0. From the URL mentioned above, the reader can find a technical preview version of the compiler in both languages. In this article, we will discuss the construction and language improvement of the development environment with C # 3.0 as the center. The download is an installation package called "LINQ Preview.msi" (which may be different) and can be installed like any other software after double-clicking, no longer repeat here. The installation package provides a plug-in (project template) and a C # compiler for Visual Studio's Beta 2 and later versions and Visual C # 2, which generate IL code that can be run directly on the. NET Framework 2.0. After the preview is installed, we can see an entry for a LINQ preview under the Visual C # node of the project type list in the New Project dialog box, and you can select some desktop project templates in the Right project template (LINQ does not support Web projects temporarily), as shown in the following illustration:

Figure 1-New Project dialog box for Visual Studio 2005 with LINQ installed

As long as the project templates in LINQ are selected, we can start writing C # 3.0 applications in Visual Studio 2005 like other applications, and the IDE will automatically select the C # 3.0 compiler for us at compile time.

Now that we're ready to start writing C # 3.0 apps, I'll explain the language enhancements that C # 3.0 brings to you later in the chapters. It is worth noting that this article is one of a series of articles, which is a total of three sections. This article is the first part, about C # 3.0 Basic Language enhancements, which are actually the basis of the other two parts, and the second part is about the lambda expression in C # 3.0, which is the natural evolutionary form of an anonymous method that not only manifests an expression as an executable method (a delegate), You can also express the expression as a data structure that can be manipulated at run time--an expression tree; The last part tells the most central and exciting content of a LINQ project--The query expression, which is the form of LINQ in C #. Also, LINQ expands the. NET Framework base Class library for SQL queries and XML queries, called DLinq and XLinq, which I'll tell you in other series.

In the course of this article, it is limited to space and provides only short snippets rather than complete code. These snippets, however, are extracted from the complete, properly compiled, and run code, which can be downloaded from here and covered in the 5th part of this article for complete code.

OK, so much nonsense, let's get into the wonderful World of C # 3.0 quickly.

Declarations with implicit types

In a declaration statement with an initializer, the type of the variable to be declared is obvious-consistent with the result type of the initialization expression. In this case, the new keyword VAR can be used in C # 3.0 instead of the type in the declaration, and the compiler infers 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 <i>: {0}", i.GetType());
Console.WriteLine ("Type of variable <d>: {0}", d.GetType());
Console.WriteLine("Type of variable <s>: {0}", s.GetType());
Console.WriteLine("Type of variable <a>: {0}", a.GetType());

The above code is syntactically consistent in C # 3.0, with the first four lines using an implicit type declaration, and then four lines of code to verify that the variables have the correct type at run time. If you run this code in Visual Studio 2005 (to be able to see the results, use CTRL+F5 to compile and start the program), you get the following result:

Type of variable <i>: System.Int32
Type of variable <d>: System.Double
Type of variable <s>: System.String
Type of variable <a>: System.Int32[]

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.