Use var keyword in variable declaration with object creation
Description
The suggestion suggests to use var keyword only if the variable is initialized with object creation (new keyword). In other words, suggestion suggests to use var keyword only if the type is directly obvious from the declaration itself.
Usage Examples
[ ] int i = new System.Int32();
[ ] List<int> list = new List<int>(10000);
[ ] List<int> list = new System.Collections.Generic.List<int>(10000);
[ ] System.Collections.Generic.List<int> list = new List<int>(10000);
[ ] MyClass myObject = new MyClass(a, b, c);
[ ] MyClass myObject = new MyClass { A = a, B = b, C = c};
[ ] using (StreamReader file = new StreamReader("C:\\myfile.txt")) {...}
Non-usage Examples
When the explicit type and the type in the object creation is not exactly the same the suggestion should not appear.
[ ] IEnumerable<int> list = new List<int>(10000);
[ ] long l = new int();
Corner Cases
A type named var in the scope
If there is a type named var in the scope, the suggestion should not appear. Because "If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration."
For more info see: http://thehumbleprogrammer.com/the-variform-var/This corner case can be ignored in the first implementation since it is very unlikely to happen.
Multiple implicitly-typed variables cannot be initialized in the same statement
E. g. this is valid:
int a = 1, b = 2, c = 3;
but this is not:
var a = 1, b = 2, c = 3;
This corner case must be supported in the first implementation. This means the suggestion must not appear in a case like this:
[ ] List<int> first = new List<int>(), second = new List<int>();
C# Suggestion definition
Description
The suggestion suggests to use
var
keyword only if the variable is initialized with object creation (new
keyword). In other words, suggestion suggests to usevar
keyword only if the type is directly obvious from the declaration itself.Usage Examples
[ ]
int i = new System.Int32();
[ ]
List<int> list = new List<int>(10000);
[ ]
List<int> list = new System.Collections.Generic.List<int>(10000);
[ ]
System.Collections.Generic.List<int> list = new List<int>(10000);
[ ]
MyClass myObject = new MyClass(a, b, c);
[ ]
MyClass myObject = new MyClass { A = a, B = b, C = c};
[ ]
using (StreamReader file = new StreamReader("C:\\myfile.txt")) {...}
Non-usage Examples
When the explicit type and the type in the object creation is not exactly the same the suggestion should not appear.
[ ]
IEnumerable<int> list = new List<int>(10000);
[ ]
long l = new int();
Corner Cases
A type named var in the scope
If there is a type named
var
in the scope, the suggestion should not appear. Because "If a type named var is in scope, then thevar
keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration." For more info see: http://thehumbleprogrammer.com/the-variform-var/ This corner case can be ignored in the first implementation since it is very unlikely to happen.Multiple implicitly-typed variables cannot be initialized in the same statement
E. g. this is valid:
int a = 1, b = 2, c = 3;
but this is not:
var a = 1, b = 2, c = 3;
This corner case must be supported in the first implementation. This means the suggestion must not appear in a case like this:
List<int> first = new List<int>(), second = new List<int>();
References
Acceptance Criteria