C# C# Basics C# Syntax Types

When declaring a variable without giving it a value, why don't we use the 'var' keyword before typing in the type?

This confuses me so I apologise if it's a dumb question but why don't we have to use 'var' before 'int' when declaring a variable?

1 Answer

Steven Parker
Steven Parker
177,847 Points

Both "var" and "int" declare a variable, but they would never both be used in the same declaration.

"Var" can only be used when you also initialize it because the compiler examines the value being assigned to determine what type of variable to create. On the other hand, "int" declares the type explicitly, so it can be used with or without initialization.