3 / 3

"Are you following me, Teo?" Tom asked.
"Yes," I answered, "but I have a question: can I replace any type with var ?"
"Yes, Teo. You can replace any type with var as long as the compiler can understand what type you mean. Here are some constraints for using var :"

1. var can only be used for local variables inside methods. You can not use var for a method's return type or a method's parameter type.

public static int Add(int a, int b) // This declaration is OK
{...}

public static var Add(int a, int b) // NOT OK: var as a return type
{...}
public static int Add(var a, var b) // NOT OK: var as a parameter's type
{...}

2. The variable must be initialized with a value if it is declared with var .

var someIntegers = new int[5]; // OK
var someIntegers; // NOT OK - no initial value
var radius = 15.789; // OK
var radius; // NOT OK - no initial value

"That just about covers var . Any more questions?" Tom asked.

"When should I use it?" I replied. " Is it considered good style to declare every local variable with var ? "
"That depends on your coding style. Some programmers prefer to use var for all possible local variable declarations. Others say that it should not be used for variables that hold a function return value. Finally, a small minority of programmers prefer to never use var at all. I would suggest using it wherever you can to start, and when you're comfortable with it, re-evaluate. "

"Thanks Tom," I said, "I'll use var where I can."
"Good. Here are some excersises to get you more familiar with var ."

keep coding