The use of implicit type local variables of c#3.0 new characteristics

Source: Internet
Author: User
Tags foreach expression variables variable
Variable first of all, this new feature of the topic based on the May c#3.0 distribution of mobile code, but the official Chinese version has not come out, combined with their own practice and understanding, hope to share some of the frontier with you, it is possible that some places will be wrong, look at throwing bricks.

In C # 3.0, you can declare a local variable implicitly type. It is when you declare that you can not specify the type of the variable, the variable is initialized by the compiler from the context of the expression of the real type, for developers, is undoubtedly simplified a lot, mainly through the new keyword VAR, Like the following example:

var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int, order> ();
In fact, the above declaration method is equivalent to the following declaration method in 1.x and 2.x:

int i = 5;
string s = "Hello";
Double d = 1.0;
int[] = new int[] {1, 2, 3};
Dictionary<int, order> orders = new Dictionary<int, order> ();

Convenience is absolute, just as we have previously implemented this feature in some other high-level languages, but the implicit type declaration of a local variable also follows certain constraints:

-When declaring a variable, the variable must be initialized.

-When you initialize the variable, you must use the corresponding expression instead of simply using an object or the collection itself, but it is legal to use the object or collection that you created with the keyword new, because it is already an expression.

-An expression that is initialized by a variable must not be a null type at compile time.

-If a local variable that will make an implicit type declaration contains more than one declaration, make sure that each initialization expression has the same compile-time type, keeps the type consistent, cannot be int, and then becomes int[.

Here are some examples of incorrect statements:

Example 1:

var x; Not initialized when declaring variable
Example 2:

var y = {1, 2, 3}; The collection itself cannot be an initialization expression
Example 3:

var z = null; Compile-time type is not allowed to be null
In the current Orcas compilation environment, you can use VAR to declare local variables and to be backward compatible, that is, the original method is available. In addition, according to MS's official documentation, the Var declaration applies to the initialization expression of a for statement and is available when the resource is obtained in a using statement, for example, to apply var to an iteration variable in a foreach statement:

var numbers = int[] {1, 3, 5, 7, 9};
foreach (var n in numbers) Console.WriteLine (n);
However, this requires that the collection type must be enumerable, and that Var can be automatically recognized by the compiler as the int type.

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.