Junior — Middle
Could you explain what the keyword 'var' means in programming?
sobes.tech AI
Answer from AI
In C#, the keyword var is used for implicit variable type declaration. The compiler infers the variable's type from the assigned value during compilation. This allows writing more concise code without explicitly specifying the type, while still keeping the variable strictly typed.
Example:
var number = 10; // number will be of type int
var text = "Hello"; // text will be of type string
It is important to note that var cannot be used without initialization, as the compiler needs to know the type immediately.