The type inference VAR,serves only to be recognized by the program only if he has to go to replace "int "string" etc.? If yes,would it not be better to always use VAR?
There are pros and cons to using the var keyword.
What are the benefits of using var
The disadvantages of using var
+ Good:
var numbers = new int[] {1, 2, 3, 4};
var stringbuilder = new StringBuilder();
var cars = new List();
var orders = new Dictionary();
+/- OK with either (but prefer explicit declaration):
int pages = 10;
string username = “john”;
var username = “john”;
var order = GetOrder(orderId); // ok if the type is Order, otherwise not
for (var x = 1; x < 10; x++)
- Bad:
var settings = GetInboxSettings(); // not obvious at all
var userId = GetUserId(); // ambigous, is this guid, string, int or a custom UserId object?
Dictionary orders = new Dictionary(); // redundant
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments