Hey guys 😀 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 😛 it’s just a simple question to get to the point. 😀

Here is how.

int  marks = 80;
string name = “John”;
string firstName;

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.. 😀

var marks = 80;
var name = “John”;

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

var firstName;

This results in a compile error. Why? I think you can guess the answer.. 😀 No? come on.. 😛 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? 😀 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?

var firstName = “John”;
firstName = 10;

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?

var firstName = null;

Whatcha say to that? 😀 can we do it? Nope. 😛 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.

var firstName = (string)null;

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.. 😀 I promise this is the last one 😛

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.

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
    // ToDo 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.

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
    // ToDo 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. 😀 In the series intro I said the post are going to be small.. But this turned out to be a big one 😀 well S**t happens. 😉 Hope you learned something. See you in the next post.

Adios amigos 😀


One thought on “Sharpen Your C#: Implicit Typing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s