C #3.0 implicit type var keyword

Source: Internet
Author: User
1. Magic VaR

Added a variable declaration VaR in C #3.0, which is similar to the Javascript var, but also different.
1. similarities. It can be used to declare any type of local variables.
2. The difference is that it only tells the compiler that the variable needs to be inferred based on the initialization expression, and can only be a local variable.

Ii. Similarities

It can declare
VaR integer = 10;
VaR name = "edisundong ";
VAR numbers = new int [] {1, 2, 3 };

Iii. Differences

VaR is just a keyword. It is not a new type in C #3.0, but is responsible for telling the compiler that the variable needs to be inferred based on the initialization expression. The preceding statement is equivalent

Int integer = 10;
String name = "edisundong ";
Int [] numbers = new int [] {1, 2, 3 };

4. Notes

1. The value must be assigned at the same time during the declaration, because the Declaration depends on the expression on the right of the value assignment number, if the following statement exists:
VaR integer;
Integer = 10;
During compilation, the implicitly typed locals must be initialized error is reported.

2. After using VAR to declare a local variable, it still has a strong type and can be tested as follows:
VaR integer = 10;
Integer = "edisundong ";
The cannot implicitly convert type 'string' to 'int' error is reported during compilation.

3. The compile-time type of the initialization expression cannot be null. the compiler cannot infer the type of the out-of-box Variable Based on null. The following statement is provided:
VaR integer = NULL;
Cannot assign' will be reported during compilation' 'To an implicitly typed local error.

4. The initialization statement must be an expression. The initialization expression cannot contain itself, but it can be a new expression (namely, an anonymous type) that contains an object or a set initiator ).
Declare as follows:
VaR coll = new hashtable ();

5. The declaration of VaR is limited to local variables and can be included in foreach, for, and using statements. The following usage is incorrect:

Class Program
{
Private var I = 10; // global private variable.
Static void main (string [] ARGs)
{

}
}
The contextual keyword 'var' may only appear within a local variable declaration error is reported during compilation.

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.