Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does the keyword var do in C#?
The var keyword in C# enables implicit type declaration where the compiler automatically determines the variable's type based on the assigned value. This feature was introduced in C# 3.0 and provides cleaner code while maintaining type safety.
Syntax
Following is the syntax for using var keyword −
var variableName = initialValue;
The compiler infers the type from the initial value. Once declared, the variable behaves as if it were declared with its actual type.
How It Works
When you use var, the C# compiler performs type inference at compile time. The variable is strongly typed − it's not dynamic or loosely typed like in some scripting languages.
Using var with Basic Types
Example
using System;
class Program {
static void Main(string[] args) {
var myInt = 5;
var myString = "Amit";
var myDouble = 3.14;
var myBool = true;
Console.WriteLine("Integer: {0} (Type: {1})", myInt, myInt.GetType().Name);
Console.WriteLine("String: {0} (Type: {1})", myString, myString.GetType().Name);
Console.WriteLine("Double: {0} (Type: {1})", myDouble, myDouble.GetType().Name);
Console.WriteLine("Boolean: {0} (Type: {1})", myBool, myBool.GetType().Name);
}
}
The output of the above code is −
Integer: 5 (Type: Int32) String: Amit (Type: String) Double: 3.14 (Type: Double) Boolean: True (Type: Boolean)
Using var with Arrays
Example
using System;
class Program {
static void Main(string[] args) {
var myIntArray = new int[] {65, 43, 88, 56};
var myStringArray = new string[] {"Apple", "Banana", "Orange"};
Console.WriteLine("Integer Array:");
foreach(var val in myIntArray) {
Console.WriteLine(val);
}
Console.WriteLine("\nString Array:");
foreach(var item in myStringArray) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Integer Array: 65 43 88 56 String Array: Apple Banana Orange
Using var with Complex Types
Example
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
var list = new List<string> {"C#", "Java", "Python"};
var dictionary = new Dictionary<int, string> {{1, "First"}, {2, "Second"}};
Console.WriteLine("List contents:");
foreach(var item in list) {
Console.WriteLine(item);
}
Console.WriteLine("\nDictionary contents:");
foreach(var kvp in dictionary) {
Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
}
}
The output of the above code is −
List contents: C# Java Python Dictionary contents: Key: 1, Value: First Key: 2, Value: Second
Key Rules for var
| Rule | Description | Example |
|---|---|---|
| Must be initialized | Cannot declare var without assignment |
var x = 5; ?var x; ? |
| Local variables only | Cannot use var for class fields | Inside methods only |
| Single variable declaration | Cannot declare multiple variables |
var x = 5, y = 10; ? |
| Cannot be null | Compiler cannot infer type from null |
var x = null; ? |
Conclusion
The var keyword in C# provides implicit type declaration through compile-time type inference. It maintains type safety while reducing code verbosity, especially useful with complex generic types. Remember that var variables are still strongly typed − the compiler determines the exact type based on the initial assignment.
