var and const
Picture a warehouse where you hand an item to the storekeeper. You can say up front: "I need a shoe box." Or you can silently hand over the boots, and the storekeeper will see which box fits.
The declaration int count = 10; names the shape of the box twice: by the word int and by the number 10. The number shows that the box is for whole numbers. C# can pick the type from the value, the way that storekeeper does. For that you write var in place of the type.
var: the type from the value
The keyword var declares a variable, and the compiler picks its type itself—from the value to the right of =. This is called type inference:
var count = 10;
var distance = 2.5;
var city = "Kazan";
var isOpen = true;
Console.WriteLine(count); // 10
Console.WriteLine(distance); // 2.5
Console.WriteLine(city); // Kazan
Console.WriteLine(isOpen); // True
count = 25;
Console.WriteLine(count); // 25Let's go through it line by line:
var count = 10;—the right side is a whole number, socountgets the typeint.var distance = 2.5;—a number with a point, typedouble.var city = "Kazan";—text in double quotes, typestring.var isOpen = true;—trueon the right, typebool.count = 25;changes the value:vardeclares an ordinary variable, and you can overwrite it.
The type is picked at compile time, before the program runs. The line var count = 10; gives exactly the same as int count = 10;. In .NET the custom is to write var when the type is visible on the right side, and the explicit type when it is not.
The type is picked once
Since count got the type int, a string no longer goes into it:
var count = 10;
count = "ten"; // ✗
Console.WriteLine(count);error CS0029: Cannot implicitly convert type 'string' to 'int'var does not make the box shapeless. count has the same shape as with an explicit int, and the compiler checks it as strictly.
Here the warehouse analogy ends. A real box can be emptied and used for another item. A variable's type never changes: only the value inside does.
Interviewers like to ask whether var makes a variable dynamically typed. In dynamically typed languages such as Python and JavaScript, one variable can hold a number first and a string later. A var variable cannot do that: its type is fixed at compile time, as if it were written out. So the answer is no: a var variable is statically typed.
Constants
The keyword const declares a constant—a named value that is known already at compile time and does not change while the program runs. It is a box sealed before the program starts:
const int DaysInWeek = 7;
const string Greeting = "Hello";
Console.WriteLine(DaysInWeek); // 7
Console.WriteLine(Greeting); // HelloA constant gives a name to a value you would otherwise repeat all over the program. The number 7 in code could mean anything, while the name DaysInWeek says at once that it is the number of days in a week.
What else to know about constants:
- the type is written explicitly:
const vardoes not compile (errorCS0822); - the value must be known at compile time—for example a number, a string,
trueorfalse, but not a variable (errorCS0133); - a constant cannot get a new value: the line
DaysInWeek = 8;does not compile (errorCS0131); - by .NET convention, constant names start with a capital letter, and so does every next word in them:
DaysInWeek,MaxScore.
Common mistake
A variable was declared with var, and its value was left for later:
var total; // ✗
total = 0;
Console.WriteLine(total);error CS0818: Implicitly-typed variables must be initializedThe program does not compile. var takes the type from the initial value, and there is no value here—nothing to pick from. Give the value right away (var total = 0;) or name the type explicitly (int total;).
Key points
vartakes the type from the initial value at compile time:var count = 10;is the same asint count = 10;.- A variable declared with
varnever changes its type, but its value changes like any variable's. - A
vardeclaration without an initial value does not compile: there is nothing to pick the type from. constdeclares a constant: its value is known at compile time and does not change.