Skip to content
Theory · Variables and types · lesson 5 of 8

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:

C#
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); // 25

Let's go through it line by line:

  1. var count = 10;—the right side is a whole number, so count gets the type int.
  2. var distance = 2.5;—a number with a point, type double.
  3. var city = "Kazan";—text in double quotes, type string.
  4. var isOpen = true;—true on the right, type bool.
  5. count = 25; changes the value: var declares 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:

C#
var count = 10;
count = "ten"; // ✗
Console.WriteLine(count);
TEXT
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:

C#
const int DaysInWeek = 7;
const string Greeting = "Hello";
Console.WriteLine(DaysInWeek); // 7
Console.WriteLine(Greeting); // Hello

A 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 var does not compile (error CS0822);
  • the value must be known at compile time—for example a number, a string, true or false, but not a variable (error CS0133);
  • a constant cannot get a new value: the line DaysInWeek = 8; does not compile (error CS0131);
  • 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:

C#
var total; // ✗
total = 0;
Console.WriteLine(total);
TEXT
error CS0818: Implicitly-typed variables must be initialized

The 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

  • var takes the type from the initial value at compile time: var count = 10; is the same as int count = 10;.
  • A variable declared with var never changes its type, but its value changes like any variable's.
  • A var declaration without an initial value does not compile: there is nothing to pick the type from.
  • const declares a constant: its value is known at compile time and does not change.