C # Tutorial: The difference between anonymous and implicit type variables

Source: Internet
Author: User
Tags anonymous object contains expression include variables variable

implicit type variable (implicitly typed local variables)

Code written like the following is an implicit type variable

var i = 5;
var str = "Csharp"
var numbers = new int[]{1,2,3};
var orders = new System.Collections.Hashtable ();
var orders1 = new Dictionary ();

var i = xxx; The function is to declare the type of I with the type of XXX. and assign a value to I.

Because you need to rely on an expression on the right side of the equal sign, you must assign values at the same time. Otherwise, the following writing will be reported to be wrong: Error implicitly typed locals must be initialized

var GG;

The following constraints should be observed when using implicitly typed local variables:

1. The declarator must include an initializer

You must include an initializer when declaring.

2, the initializer must is an expression. The initializer cannot is an object or collection initializer by itself, but it can is a new expression that includes a O Bject or collection initializer.

Initialization must be an expression. An initialization expression cannot contain itself, but it can be a new expression (that is, an anonymous type) that contains an object or a collection initializer.

3, the Compile-time type of the initializer expression cannot be the null type.

The compile-time type of the initializer expression may not be a null (NULL) type.

Like what:

var GG = null; This line of code will be reported as error: cannot assign ' to a implicitly typed local

4. If The local variable declaration includes multiple declarators, the initializers must all have the same compile-time Ty Pe.

If a local variable declaration contains more than one declaration, the declarations must have the same compile-time type.

For example, the following code:

var n = "Java"
var m = 4;
var ff = m + N;

Console.WriteLine (FF);
Console.WriteLine (ff. GetType ());

The printed information will be:

4java
System.String

5. The initializer cannot refer to itself. (cannot lift itself in initialization)

The scope of the implicit type variable is not available everywhere. Can only be used in the following four situations

1. Local variable declaration

2. Variable declaration in the For statement)

3. The using statement initializes the variable.

4. Iterator type declaration in foreach)

For example, the following code will have an error:

Class Program
{
private var tmp = "Java"
}
The contextual keyword ' var ' may only be appear within a local variable declaration

An implicit type variable is actually a work done by the compiler (the type of the compiler local variable derives from the expression that initializes them), so

var i = 5; This code, compiled with Reflector and then decompile to see is:

int i = 5;

Anonymous type (Anonymous Types)

Anonymous type, a tuple (tuple) type that is automatically deduced and created from the object initializer.

var o = new {Name = ' Hello ', age = 23};
var intarr = new[] {3,1,4,1,5};

is an anonymous type of writing.

Note:

var intarr = new[] {3,1,4,1,5}; is an anonymous type and, of course, an implicit type variable

var intarr = new int[] {3,1,4,1,5}; is an implicitly-typed variable

the difference between an anonymous type and an implicit type variable

An implicit type variable is one that we can infer from the expression on the right side of the equals sign.

Anonymous type means that, based on this type of initialization function, we can push the export and create an instance of this type.

These two features are often at work.

For example: new {Name = "Hello", age = 23} The type of expression is anonymous, so o becomes that anonymous type.

The Name = "Hello" can also be considered an implicit type variable.

But no one is going to use var i = 10; this looks like JScript syntax, so VAR is basically for anonymous types.

Description:

The above information is based on Orcas Beta 1. Perhaps the official version of the time, will change.



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.