Take the following snippet:
List distances = new List();
Was the redundancy intended by the language designers? If so, wh
You could always say:
var distances = new List<int>();
A historical artifact of static typing / C syntax; compare the Ruby example:
distances = []
What's redudant about this?
List<int> listOfInts = new List<int>():
Translated to English: (EDIT, cleaned up a little for clarification)
Not really verbose when you think about what it does.
Of course there is an alternative:
var listOfInts = new List<int>();
Here we are using C#'s type inference, because you are assigning to it immediately, C# can figure out what type you want to create by the object just created in the heap.
To fully understand how the CLR handles types, I recommend reading CLR Via C#.
Use var if it is obvious what the type is to the reader.
//Use var here
var names = new List<string>();
//but not here
List<string> names = GetNames();
From microsofts C# programing guide
The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code
Because we're addicted to compilers and compiler errors.
I see one other problem with the using of var for laziness like that
var names = new List<string>();
If you use var, the variable named "names" is typed as List<string>,
but you would eventually only use one of the interfaces inherited by List<T>.
IList<string> = new List<string>();
ICollection<string> = new List<string>();
IEnumerable<string> = new List<string>();
You can automatically use everything of that, but can you consider what interface you wanted to use at the time you wrote the code?
The var keyword does not improve readability in this example.