Some notes about Implicitly typed variables
During some C# LINQ project i have discovered some limitations and some things that can be done when working with the var new syntax,Here is a brief summery:
- When declaring a variable , you must assign its value on the declaration line.
//Implicitly-typed local variables must be initialized
var someVariable;
- Again: must be done exactly when declaring
//Implicitly-typed local variables must be initialized
var someVariable;
someVariable = 1;
Cannot assign null as an Initial value
//Cannot assign <null> to an implicitly-typed local variable
var oObj = null;
4. However, after initialized can be null.
var oObj = new Object();
oObj = null;
5. It is permitted to return a var value as long as the function is typed, such as:
private int GetNumber()
{
var x = 5;
return x;
}
6. Another limitation is , you cannot define a class member as var!!!