The var Keyword
In this page:
The var Keyword
var lets the compiler infer a variable's type from its initializer at compile time. The variable is still statically typed — var is not dynamic typing, it's just a shorthand for the programmer. It must always be initialized on the same line it is declared.
Warning: var x; without an initializer is a compile error — the compiler has nothing to infer the type from.
Example: The var Keyword
using System;
class Program
{
static void Main(string[] args)
{
var count = 10;
var name = "Alice";
var price = 19.99;
Console.WriteLine(count.GetType());
Console.WriteLine(name.GetType());
Console.WriteLine(price.GetType());
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: