I’ve come to really enjoy using the “var” keyword in C#. Although it’s been written about many times, I want to clarify what it is.
C# is a strongly-typed language, which means that every variable you declare must have a specified type. You must say it’s an integer or a string or a Thing. “var” does not change that.
Here is a typical piece of C# code:
List<Thing> myThingList = new List<Thing>();
Notice something redundant in there? We said List<Thing> twice.
Since the right side of that assignment will always result in a List<Thing>, why do we need to say so on the left side? Thus:
var myThingList = new List<Thing>();
The compiler knows that myThingList will always be a List<Thing>. So it can unambiguously (strongly) type it as such. No need for us to spell it out.
So I find that var is a nice thing for more terse code. And if I decide that myThingList should actually be an array, I just need to change it one place.