Implicit Variables
-
I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.
var viewModel = new StoreIndexViewModel {
NumberOfGenres = genres.Count(),
Genres = genres};I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall
-
I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.
var viewModel = new StoreIndexViewModel {
NumberOfGenres = genres.Count(),
Genres = genres};I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall
-
I have been studying C# for 6 months now. Perhaps I should have but I haven't yet run into the use of var for implicit variables and - in the following example - the subsequent replacing of parentheses with braces for enclosing the parameters.
var viewModel = new StoreIndexViewModel {
NumberOfGenres = genres.Count(),
Genres = genres};I assume there is a particular reason for this? Any help would be appreciated. Thanks. Darrall
You can make the code more readable (though that's subjective). E.g
var x = new Dictionary<string, List<person>>();
instead of
Dictionary<string, List<person>> x = new Dictionary<string, List<person>>();
You can use them in LINQ queries where the result type is not obvious - the compiler will get it right. You can find out the type by hovering over the var in VS - it may not be what you think. Worth experimenting with. This is one of those topics that cause 'religious' debates or flame wars - though that seems to have died down. Some people say 'Never' others 'Always'. :-D
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
You can make the code more readable (though that's subjective). E.g
var x = new Dictionary<string, List<person>>();
instead of
Dictionary<string, List<person>> x = new Dictionary<string, List<person>>();
You can use them in LINQ queries where the result type is not obvious - the compiler will get it right. You can find out the type by hovering over the var in VS - it may not be what you think. Worth experimenting with. This is one of those topics that cause 'religious' debates or flame wars - though that seems to have died down. Some people say 'Never' others 'Always'. :-D
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.