SSW Foursquare

Do you use "var"?

Last updated by Bryden Oliver [SSW] about 2 months ago.See history

In C# the "var" keyword can be used instead of providing an explicit type when declaring a variable. The type is then inferred from the initial assignment of the variable.

It is just a short hand to save developers from typing out the type of a variable.

List<string> items = new List<string>();

Figure: Bad example - You should just use "var" instead of "List"

var item = new List<string>();

Figure: Good example - Using "var" to save a few keystrokes and reduce repetition

This can be kept consistent by creating an editorconfig file which can then generate compile time warnings.

An example editor config, look for the var preferences.

Bryden Oliver
We open source. Powered by GitHub