Implicit types of new features of c#3.0 language

Source: Internet
Author: User

20.1 Implicit type

Added a variable declaring Var in c#3.0, which is similar to JavaScript's Var, but it's also different. The same thing is that it can declare any type of local variable with VAR, except that it only tells the compiler that the variable needs to infer the type of the variable based on the initialization expression and can only be a local variable.

The Declaration and use of 20.1.1 implicit type local variables

In C # 3.0, a new keyword called var is introduced. var allows you to declare a new variable whose type is implicitly inferred from the expression used to initialize the variable, that is, when declaring, you do not need to define a type, and it infers its type based on its initializer expression. Therefore, we call it an implicit type. If you can declare variables like this:

var i = 10; Declares a local variable.

This line uses the 10来 initialization variable i. Note that I is strongly typed to an integral type, it is not an object or a VB6 variable, nor is it loaded with other objects or variables.

In order to guarantee the strongly typed nature of variables declared using the var keyword, c#3.0 requires you to assign an initial value to the variable (initialization) and put it on the same line. Similarly, the initialization value must be an expression and cannot be an object or a collection initializer or null. If multiple declarations exist on the same variable, they must be considered as the same type at compile time.

The following statements are legal:

var i = 5;

var s = "Hello";

var a = new[] { 0, 1, 2 };

var expr =

      from c in customers

      where c.City == "London"

      select c;

var anon = new { Name = "Terry", Age = 34 };

var list = new List<int>();

The last thing we need to know is that the VAR keyword does not mean "variant", nor does it mean that the variable is a loosely typed or late-bound variable. It simply indicates that the most appropriate type is determined and assigned by the compiler.

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.