Type inference and default values
The var keyword lets the compiler infer the type of an ordinary local variable from its initializer. The variable still has a fixed static type. default gives a type’s default value; it is not the same as declaring an unassigned local variable. Understanding this difference prevents several common beginner compilation errors.
var count = 12;var price = 2.50m;count = 18;System.Console.WriteLine(count.GetType().Name);System.Console.WriteLine(price.GetType().Name);System.Console.WriteLine(count);The inferred types are System.Int32 and System.Decimal. Changing count to 18 does not change its type.
Expected output (reasoned, not executed):
Int32Decimal18Inference chooses a type at compile time
var copies = 12; infers int, while var price = 2.50m; infers decimal. The initializer provides enough type information for the compiler. An ordinary local var declaration needs an initializer with an inferable type; var copies; and var missing = null; do not supply one. Prefer explicit types where the initializer makes the type unclear. Inference is not dynamic typing.
Default expressions and definite assignment
default(int) evaluates to zero; default(bool) to false; default(char) to the zero code unit; default for a reference type is null; default for a nullable value type is no value. A target-typed default literal also works when the expected type is known, as in int count = default;. Do not assume a declaration like int count; initializes a local automatically. The compiler requires definite assignment before reading a local. Fields and array elements receive default initialization, topics explored later.
Type is not the same as current value
A variable can change its value while retaining its type. In the example below, count is inferred as int and can receive another int, but not a string. GetType().Name is used here as a small diagnostic aid for the non-null sample values; it reports the runtime type name. Static and runtime types can differ for reference types, so this is not a general static-type inspection tool.
Examples and walkthroughs
int count = default;bool available = default(bool);string? note = default;System.Console.WriteLine(count);System.Console.WriteLine(available);System.Console.WriteLine(note == null);The variables are initialized with zero, false and a null reference. The last line checks null without dereferencing the string.
Expected output (reasoned, not executed):
0FalseTrueint total;System.Console.WriteLine(total);Intentionally invalid: the compiler cannot establish that total has been assigned. Fix by initializing total or assigning to it before the read; no execution is expected.
The assignment precedes the read, so this simple local satisfies definite assignment.
Expected output (reasoned, not executed):
5Common mistake and correction
Avoid var total = default; without a target type. Use int total = default; or var total = default(int);. Do not assume var can later hold any type. An ordinary local must be assigned before reading, even if its type has a default value.
Try it yourself
Declare an inferred integer initialized to 10, update it to 14, then declare an explicitly typed Boolean with its default value. Print both. Explain why var number = null; is not a suitable declaration for a missing integer.
Next: Arithmetic and integer division. These examples are static study material; actual C# compilation and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type inference and default values — knowledge check
Apply the lesson concepts to the following scenarios.
What type does var number = 10; infer?
After var number = 10;, is number = "ten"; allowed?
What does default(bool) evaluate to?
Why does int total; followed by printing total fail?
Which declaration gives default a known target type?