C# | Implicitly Typed Local Variables - var
Last Updated :
10 Apr, 2024
Implicitly typed variables are those variables that are declared without specifying the .NET type explicitly. In an implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable.
The implicitly typed variable concept is introduced in C# 3.0. The implicitly typed variable is not designed to replace the normal variable declaration, it is designed to handle some special-case situations like LINQ(Language-Integrated Query).
Question: Why it is termed Local?
Answer: It is not allowed to use var as a parameter value or return type in the method or define it at the class level etc. because the scope of the implicitly typed variable is local.
Example of Implicitly Typed Local Variables
C#
// C# program to illustrate
// implicitly typed local variable
using System;
public class GFG {
// declaring and initializing implicitly
// typed local variable at class level.
// It will give compile time error
var imp = 15;
// Main method
static public void Main()
{
// trying to print the value of 'imp'
Console.WriteLine(GFG.imp);
}
}
Compile-Time Error:
prog.cs(10,2): error CS0825: The contextual keyword `var' may only appear within a local variable declaration
Important Points of Implicitly Typed Local Variables
- Implicitly typed variables are generally declared using var keyword as shown below:
var ivariable = 10;
- In implicitly typed variables, you are not allowed to declare multiple var in a single statement as shown below:
var ivalue = 20, a = 30; // invalid
- It is not allowed to use var as a field type in class level.
- It is allowed to use the expression in var like:
Var b;
b -= 39;
- In C#, one cannot declare implicitly typed variable without any initialization like:
var ivalue; // invalid
- It is not allowed to use a null value in implicitly typed variable like:
var value = null; // invalid
- The initializer cannot contain any object or collection, it may contain a new expression that includes an object or collection initializer like:
// Not allowed
var data = { 23, 24, 10};
// Allowed
var data = new int [] {23, 34, 455, 65};
- It is not allowed to initialize implicitly typed variable with different types more than one type like:
// It will give error because
// the type of the value is different
// one is of string type and another
// one is of int type
var value = "sd"
value = new int[]{1, 2, };
Example 1
C#
// C# program to illustrate the
// concept of implicitly typed variable
using System;
public class GFG {
// Main method
static public void Main()
{
// Declaring and initializing
// implicitly typed variables
var a = 50;
var b = "Welcome! Geeks";
var c = 340.67d;
var d = false;
// Display the type of the variables
Console.WriteLine("Type of 'a' is : {0} ", a.GetType());
Console.WriteLine("Type of 'b' is : {0} ", b.GetType());
Console.WriteLine("Type of 'c' is : {0} ", c.GetType());
Console.WriteLine("Type of 'd' is : {0} ", d.GetType());
}
}
OutputType of 'a' is : System.Int32
Type of 'b' is : System.String
Type of 'c' is : System.Double
Type of 'd' is : System.Boolean
Example 2
C#
// C# program to illustrate the
// use of implicitly typed variable
using System;
class GFG {
// Main method
static public void Main()
{
// explicitly typed variables
int Base = 13;
int Height = 20;
int Area = (Base * Height) / 2;
// Display result
Console.WriteLine("Height of triangle is: " + Height
+ "\nBase of the triangle is: " + Base);
Console.Write("Area of the triangle is: {0}", Area);
}
}
OutputHeight of triangle is: 20
Base of the triangle is: 13
Area of the triangle is: 130
Note: Implicitly typed local variables can be used as a local variable in a function, in foreach, and for loop, as an anonymous type, in LINQ query expression, in using statement etc.
Similar Reads
C# | Types of Variables A variable is a name given to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used. It is the basic unit of storage in a program. The value stored in a variable can be changed during program exec
10 min read
C# Scope of Variables The part of the program where a particular variable is accessible is termed the Scope of that variable. A variable can be defined in a class, method, loop, etc. In C/C++, all identifiers are lexically (or statically) scoped, i.e., the scope of a variable can be determined at compile time and is inde
4 min read
Difference between var and dynamic in C# Implicitly Typed Local Variables â var are those variables which are declared without specifying the .NET type explicitly. In implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable. The implicitly ty
4 min read
Local Variable in C In C language, a variable declared within a function or a block of code is called a local variable. Local variables are frequently used to temporarily store data in a defined scope where they can be accessed and manipulated. They are stored in the memory stack, Once the function or block of code in
3 min read
Difference between var keyword and short declaration operator in Golang A variable is a storage location or place holder used for holding the value. It allows us to manipulate and retrieve the stored information. There are two ways to declare the variables in Golan which are as follows: var varName type = valuevarName := valueDifference between var keyword and short dec
4 min read