What is it?

Implicit typing is a language feature of C# which allows you to declare variables without explicitly stating the type. Here is an example of an explicit type declaration followed by an implicit type declaration:

string firstName = "Jim";
var firstName = "Jim";

The var keyword allows for a cleaner line of code without losing or obscuring the intent. We still know we are creating the string “Jim” and that we can access it using “firstName”.

The benefits of this are evident if you consider a typical block of C# code, where we are newing up a bunch of objects. It typically has a kludgy cluttered look to it:

BusinessObject businessObject = new BusinessObject();
OtherBusinessObject otherBusinessObject = new OtherBusinessObject();
BusinessObjectProvider businessObjectProvider = new BusinessObjectProvider();

The explicit type declaration here is un-necessary and harder to read. We can use var again to clean things up:

var businessObject = new BusinessObject();
var otherBusinessObject = new OtherBusinessObject();
var businessObjectProvider = new BusinessObjectProvider();

Just like the previous example, the code is cleaner and we have not lost any information.

Can I use it everywhere?

You can only use it to declare a variable if you are initializing the variable in the same line. That way the compiler can infer the type and convert it behind the scenes to an explicit declaration. The following lines will raise compiler errors because the compiler can’t possibly know what type “someObject” is:

var someObject;
var someObject = null;

It’s important to note that C# is still a strongly typed language, and by use of the var keyword we are not declaring dynamically typed objects.

When should I use it or not use it?

So far this feature sounds like a dream. Reduced code verbosity while maintaining the usual compile time type checks, what could possibly go wrong?

Well, there are ways in which this feature can be misused or overused.

You should use it when the assignment (things to the right of the equals sign) makes it obvious what the type of the variable is. That way you gain all the benefits of cleaner, less cluttered code without losing any information.

var someObject = someObjectFactory.Create();

This is an example of implicit typing being misused. We don’t know what type “someObject” is by just reading this line of code. The gain in readability is offset by the loss of information. In this case we should explicitly declare the variable so it is clear we are declaring an object of type “SomeObject”.

SomeObject someObject = someObjectFactory.Create();

The Java language designers considered the possibility of the above case so unacceptable that they have avoided implementing implicit typing at all. I think that’s a little too conservative and Java desperately needs features like this to reduce it’s verbosity.

As small and inconsequential this feature is, it’s one of many reasons why C# is much more pleasant to work with than Java.

Be explicit with your number types

One common thing I see is the use of var with number types, this opens your program up to unexpected overflows because the compiler will default to use int, even if the next line after the declaration has you adding int.MaxValue! Even if you “know” your number couldn’t possibly overflow, it’s a good habit to be explicit and to always put some thought into sizing your number variables appropriately.

I’ve done online programming challenges for job interviews where they catch this kind of careless out by putting your code through test cases which lead to overflow if you haven’t thought carefully about the correct type to use, so be careful!

var someNumber = 2;
someNumber += int.MaxValue;
Console.WriteLine(someNumber.GetType()); // System.Int32
Console.WriteLine(someNumber); // -2147483647