var object type declaration introduced in the. NET Framework

Source: Internet
Author: User
Tags object copy expression integer net variables string variable
After the VS 2008 is installed, in the background code, the ReSharper plugin prompts suggestions for all local variables in the background, displaying the need for "use implicitly typed variable declaration", and using the code to modify the proposal to find " Use Var's hint, literally, will soon be known this is the new mechanism introduced in. NET framework3.5, which is the automatic type setting of local variables, depending on the type of object initialized on the right side of the equals sign. On the Internet to check the relevant explanations, recorded here:
One, the magic of Var
Added a variable declaring Var in c#3.0, which is similar to JavaScript's Var, but it's also different.
1. At the same point, he can declare any type of local variable with var.
2. At different points, he is only responsible for telling 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.
second, the same point
He was able to declare:
Copy CodeThe code is as follows:
var integer = 10;
var name = "Edisundong";
var numbers = new int[] {1, 2, 3};

three, different points

var is just a keyword, he is not a new type in c#3.0, but is responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression, which is equivalent to:
Copy CodeThe code is as follows:
int integer = 10;
String name = "Edisundong";
int[] numbers = new int[] {1, 2, 3};

Four, note the point

1. The declaration must be assigned at the same time, because the declaration relies on the expression to the right of the assignment number if the following statement is present:
Copy CodeThe code is as follows:
var integer;
Integer = 10;

The implicitly typed locals must be initialized error is reported at compile time.

2. After using VAR to declare a local variable, he still has a strong type that can be tested as follows:
Copy CodeThe code is as follows:
var integer = 10;
Integer = "Edisundong";

The cannot implicitly convert type string to int error is reported at compile time.

3. The compile-time type of the initializer expression cannot be a null (NULL) type, and the compiler cannot infer the type of the local variable based on null, such as the following statement:
Copy CodeThe code is as follows:
var integer = null;

Compile-time will report cannot assign <null> to a implicitly typed local error.

4. The initialization statement must be an expression, and the initialization expression cannot contain itself, but can be a new expression (that is, an anonymous type) that contains an object or collection initializer. If you can declare this:
Copy CodeThe code is as follows:
var coll = new Hashtable ();

5. Var's declaration is limited to local variables and can also be included in foreach, for, 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)
{ }
}

Compile-time will report the contextual keyword Var appear within a local variable declaration error.

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.