Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 5. March 2009 15:54

My first reaction when I learnt that the keyword var had been introduced in C# 3.0 was horror. It comes in part from my past as a VBA developer, which has a similar-looking and named "variant" type. "Variant" is pretty much the equivalent of "object" in C#, which explains my worry: on the surface, a statement like the one below looks like you gave up on all the goodness of type-safety.

var myInstance = new MyClass();

I quickly learnt that var != variant, and that var IS type safe - and after reading the Handbook from the Department of Declaration Redundancy Department, I started using var to instantiate new objects:

EncoderReplacementFallbackBuffer buffer = new EncoderReplacementFallbackBuffer();
var buffer = new EncoderReplacementFallbackBuffer();



Comment RSS