Sharpen Your C#: Implicit Typing

Kasun Kodagoda
The KVK Blog
Published in
4 min readDec 14, 2014

Hey guys :D welcome to the first post in Sharpen Your C# series. Its about Implicit Typing. So, lets get cracking… ;)

To start off with a question, how do we define variables in C#? Now you must be laughing at me now. Is that a question? Who do you think we are toddlers? Well hold your houses :P it’s just a simple question to get to the point. :D

Here is how.

[code language=”csharp”]
int marks = 80;
string name = “John”;
string firstName;
[/code]

First there is a type, then there is the variable name and then you can optionally initialize it with a value or not. Why is it like that?

You know C# is a Strongly Typed language, that means you need to explicitly define the type of a variable before using it and the type of the variable cannot be changed during runtime ( the value of the variable can be changed). But with introduction of C# 3.0 (Visual Studio 2008 with .Net 3.5) a new type was introduced. That is var

Variables can now be defined with var instead of using a type. These variables defined using var is implicitly typed. But they are static that means at runtime the type can’t change. And with var declarations are resolved at compile time, that means the type of the variable is deduced by the compiler at compile time using the value the variable is initialized with.

So how can we use var? easy.. :D

[code language=”csharp”]
var marks = 80;
var name = “John”;
[/code]

This compiles perfectly fine without any compile errors. As usual we define the variable name and initialize it with a value. But look below.

[code language=”csharp”]
var firstName;
[/code]

This results in a compile error. Why? I think you can guess the answer.. :D No? come on.. :P remember what I said earlier? The type of the variable is deduced at runtime with the value used to initialize it. So in this case where is the value to determine the type? :D that’s right to use var you need to initialize it with a value so the compiler can assign the correct type to the variable at run time. So you must initialize the variable with a value when using var

Ok then can you do this?

[code language=”csharp”]
var firstName = “John”;
firstName = 10;
[/code]

Nope, you can’t. Don’t be fooled. Just because you used var doesn’t mean you can assign a different type of a value later in the code like in some other languages. Some languages like JavaScript allow this. But C# DOES NOT.

Ok then smarty-pants can you do this?

[code language=”csharp”]
var firstName = null;
[/code]

Whatcha say to that? :D can we do it? Nope. :P But why? The answer is simple. Null can be assigned to many reference types. That mean a string can be null, a custom type you created can be null etc. like that null can mean many types. So the compiler can’t figure out what will be the type at compile time. So this results in a compile error. But you can do this.

[code language=”csharp”]
var firstName = (string)null;
[/code]

This compiles without any problems. The type cast makes sure that the null is correctly associated with a string type. So the compiler can now deduce the type of the variable using the type cast.

So the next question is.. God why so many questions you may ask.. :D I promise this is the last one :P

Why should you use var?

One benefit of using var is to reduce the amount of typing we do. .Net framework has some types with long names and you also might have some custom types with long names. So by using var you can reduce the amount you type by removing the long type name from the variable definition. This makes you code more readable. Look at the following example.

[code language=”csharp”]
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
// ToDo code
}
[/code]

I use this code type of code a lot when developing Window Phone apps. And that IsolatedStorageFile type is long to type and I can use var to reduce typing.

[code language=”csharp”]
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
// ToDo code
}
[/code]

And also to use Anonymous Types in C# you need to use var. What the hell is an anonymous type? You may ask. I’ll explain that in a later post.

So that’s it boys and girls. That’s it about Implicit Typing. Hope you enjoyed it. :D In the series intro I said the post are going to be small.. But this turned out to be a big one :D well S**t happens. ;) Hope you learned something. See you in the next post.

Adios amigos :D

--

--

Kasun Kodagoda
The KVK Blog

Passionate about technology and computer science. Crazy for all things mobile and Technical Lead at @99XTechnology