To me, the antipathy towards var
illustrates why bilingualism in .NET is important. To those C# programmers who have also done VB .NET, the advantages of var
are intuitively obvious. The standard C# declaration of:
List<string> whatever = new List<string>();
is the equivalent, in VB .NET, of typing this:
Dim whatever As List(Of String) = New List(Of String)
Nobody does that in VB .NET, though. It would be silly to, because since the first version of .NET you've been able to do this...
Dim whatever As New List(Of String)
...which creates the variable and initializes it all in one reasonably compact line. Ah, but what if you want an IList<string>
, not a List<string>
? Well, in VB .NET that means you have to do this:
Dim whatever As IList(Of String) = New List(Of String)
Just like you'd have to do in C#, and obviously couldn't use var
for:
IList<string> whatever = new List<string>();
If you need the type to be something different, it can be. But one of the basic principles of good programming is reducing redundancy, and that's exactly what var does.