Var object type declaration introduced in. net framework

Source: Internet
Author: User

After vs 2008 is installed, in the background code, the resharper plug-in prompts and recommends all the local variables in the background, and displays the need to use "use implicitly typed local variable declaration". It is recommended to modify the code, I found a prompt for "use Var". Through literal meaning, I will soon be able to know that this is. net framework3.5 introduces a new mechanism, that is, to automatically set the type of local variables, the type depends on the type of the object initialized on the right of the equal sign. I checked the relevant explanations on the Internet and recorded them here:
I. Magic var
Added a variable declaration var in C #3.0, which is similar to the JavaScript var, but also different.
1. similarities. He can use var to declare any type of local variables.
2. The difference is that he is only responsible for telling the compiler that the variable needs to be inferred based on the initialization expression, and can only be a local variable.
Ii. Similarities
He can declare:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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. Suppose there is the following statement:
Copy codeThe Code is as follows:
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 perform the following tests:
Copy codeThe Code is 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 available:
Copy codeThe Code is as follows:
Var integer = null;

The Cannot assign <null> to an implicitly typed local error is reported during compilation.

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:
Copy codeThe Code is 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:
Copy codeThe Code is as follows:
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.

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.