Var Assignment In C#

Implicitly Typed Local Variables (C# Programming Guide)

Local variables can be declared without giving an explicit type. The keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library. For more information about how to initialize arrays with , see Implicitly Typed Arrays.

The following examples show various ways in which local variables can be declared with :

It is important to understand that the keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.

The keyword may be used in the following contexts:

  • On local variables (variables declared at method scope) as shown in the previous example.

  • In a for initialization statement.

  • In a foreach initialization statement.

  • In a using statement.

For more information, see How to: Use Implicitly Typed Local Variables and Arrays in a Query Expression.

var and Anonymous Types

In many cases the use of is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions. For more information, see Anonymous Types.

From the perspective of your source code, an anonymous type has no name. Therefore, if a query variable has been initialized with , then the only way to access the properties in the returned sequence of objects is to use as the type of the iteration variable in the statement.

The following restrictions apply to implicitly-typed variable declarations:

  • can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.

  • cannot be used on fields at class scope.

  • Variables declared by using cannot be used in the initialization expression. In other words, this expression is legal but this expression produces a compile-time error:

  • Multiple implicitly-typed variables cannot be initialized in the same statement.

  • If a type named is in scope, then the keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

You may find that can also be useful with query expressions in which the exact constructed type of the query variable is difficult to determine. This can occur with grouping and ordering operations.

The keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is . As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity.

However, the use of does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses only when it is required.

See Also

C# Reference
Implicitly Typed Arrays
How to: Use Implicitly Typed Local Variables and Arrays in a Query Expression
Anonymous Types
Object and Collection Initializers
var
LINQ Query Expressions
LINQ (Language-Integrated Query)
for
foreach, in
using Statement

#967 – Assigning a Value to a Variable

November 5, 2013Leave a comment

You store a value in a variable, or assign the value, use the = operator.  The = operator is known as the simple assignment operator.

When using the simple assignment operator, the value of the right operand is computed and the result is stored in the variable that appears as the left operand.

myInteger = 5; // 5 stored in myInteger myInteger = 8 + 3; // 11 stored in myInteger

The right operand is an expression containing one or more operands and operators that is evaluated to determine a resulting value.  Operands within the expression can be constants, variables, properties or the results of function calls.

myInteger = (12 + (8 * TripleThisNumber(2))) / myDog.Age;

The left side of an assignment statement can specify a variable, a property, or an indexer access.

myDog.Age = 12; myArray[2] = 42;
Categories: 1

0 Replies to “Var Assignment In C#”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *