Implicitly Typed Local Variable In .NET

An implicitly typed local variable is a variable that can be declared without specifying the .NET type explicitly. In an implicitly typed local variable declaration, the type of the local variable is obtained from the expression used to initialize the variable. The type of the variable is inferred at compile time from the expression on the right side of the initialization statement.

Let us take some examples of implicitly typed local variables.

var Int_ = 123;
var String_ = "Hello World";
var Array_ = new int[]
{
    10,
    11,
    12
};
var Stack_ = new Stack();

The above implicit typed variables are similar to the following explicit typed variables.

int Int_ = 123;
string String_ = "Hello World";
int[] Array_ = new int[]
{
    10,
    11,
    12
};
Stack Stack_ = new Stack();

The “Implicitly Typed Local Variable” is a combined form of two terms, “Implicitly Typed” and “Local Variable”.

Implicit typed

The .NET Framework 2.0 or earlier describes that all variables must be declared explicitly. With each new variable name, the data type must be used as a prefix.

Example

var Int_ = 123;
var String_ = "Hello World";

The .NET Framework 3.0 introduced the “Implicitly Typed Local Variable” that instruct the compiler to identify the data type of variable according to its initial declaration.

var Int_ = 123; // Integer Type
var String_ = "Hello World"; // String Type

Local Variable

The “Implicitly Typed Local Variable” may only be used within class members such as methods and property. It may not be used in any element of a class where the variable could be a part of the public interface. If we use “Implicitly Typed Local Variable” for global scope then the compiler will throw an error.

Example

Example

In the above image, we tried to declare a var variable as a global variable, but the compiler throws an error that the var may only appear in the local scope.

Restrictions for “Implicitly typed local variable”

Now we consider some restrictions for “Implicitly Typed Local Variable” that make it an unreliable variable for general purpose use.

The declaration must include an initializer

We can’t declare an Implicitly typed Local Variable without any initialization. The declaration must include the initializing.

Example

Error

The initializer must be an expression

The initializer can’t be a collection or an object, but it can be a new expression that includes an object or collection initializer.

Example

var Array_ = new[] // Error Cannot initialize an implicitly-typed local variable with an array initializer
{
    10,
    11,
    12
};

var Array_ = new int[] // No Error
{
    10,
    11,
    12
};

Implicitly typed Local Variable can’t be null

We can’t use a null type for implicitly typed local variables.

 Local variables

The initializer must have the same data type

If we declare the implicitly typed local variable multiple types then each time initializers must have the same compile-time type. The implicitly-type local variable can’t be initialized with different types more than one time.

Example

Data type

Can’t be used as a parameter or return type in the method.

Due to the local scope, we can’t use var as a parameter or as a return type for any method.

 Parameter

Uses of Implicitly typed local variable

  1. If we know the type that we want to use then we don’t have any need for var but if we don't have any idea about type then we can use var variable. So use var for anonymous types.
  2. Var is also useful for LINQ (Language Integrated Query) queries.

Where to use?

Only local-variable-declaration, for-initializer, resource-acquisition, and for each-statement can contain implicitly typed local variable declarations.

Thanks for reading the article.


Recommended Free Ebook
Similar Articles