Implicitly Typed Local Variable In .NET

Implicitly Typed Local Variable

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 obtain 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 initialization statement.

Let us take some example of implicitly typed local variable.

  1. var Int_ = 123;  
  2. var String_ = "Hello World";  
  3. var Array_ = newint[]   
  4. {  
  5.     10,  
  6.     11,  
  7.     12  
  8. };  
  9. var Stack_ = newStack();  
Above implicit typed variables are similar to the following explicit typed variables;
  1. int Int_ = 123;  
  2. string String_ = "Hello World";  
  3. Array Array_ = newint[]   
  4. {  
  5.     10,  
  6.     11,  
  7.     12  
  8. };  
  9. Stack Stack_ = newStack();  
The “Implicitly Typed Local Variable” is a combine form of two terms , “Implicitly Typed” and “Local Variable”.

Implicit typed:

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

Example:
  1. int Int_ = 123;   
  2. string 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.
  1. var Int_ = 123; //Integer Type  
  2. 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 compiler will throw an error.

Example:

example

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

Restrictions for “Implicitly Typed Local Variable”:

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

Declaration must include an initializer:

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

Example:

ex

Initializer must be 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:
  1. var Array_ =   
  2. {  
  3.     10,  
  4.     11,  
  5.     12  
  6. }; //Error Cannot initialize an implicitly-typed local variable with an array initializer  
  7.   
  8. var Array_ = newint[]  
  9. {  
  10.     10,  
  11.     11,  
  12.     12  
  13. }; // No Error  
Implicitly typed Local Variable can’t be null:

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

example

Initializer must have same data type:

If we declare the implicitly typed local variable multiple type 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:

example

Can’t use as parameter or return type in method:

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

example

Uses of Implicitly Typed Local Variable:

 

  1. If we know the type that we want to use then we don’t have any need of 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 foreach-statement can contain implicitly typed local variable declarations.

Thanks for reading the article.

Up Next
    Ebook Download
    View all
    Learn
    View all