What is the "var" keyword in C#?
The “var” keyword shifts the responsibility of selecting the data type of a variable from the programmer to the compiler. How?
- If the “var” keyword is used before the name of a variable, its type need not be defined.
- When the code is run, the compiler decides the data type for the variable itself.
A “var” type variable cannot be null. It must always have a value. Think of it this way- you can’t keep a hidden object on the desk and guess what it is. The same way, you can’t have a null variable and expect the compiler to decide its data type.
Usually, the “var” keyword is used when the data type of the variable is exceptionally long and it’s tedious to keep repeating it. Moreover, in array lists when there are elements of multiple data types, it’s efficient to declare the type as “var” and let the compiler decide the type of each element.
using System;class varKeyword{static void Main(){var i=5;var j=10;Console.WriteLine(i+j);}}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved