Friday, February 11, 2011

Dynamic typing and late binding in C# 4.0


DARE TO SHARE?
Static binding in C#

Back in C# 2.0 days, there was only static binding, or, static typing supported in the language.

That is, unlike JavaScript, or, PHP, we had to specify the type of the variables at compile time. As long as compiler was unhappy with the variable type declarations, we had to make correction of the types in order to be able to compile and run the program.

Some examples of static typing is:

int a = 5;
string s = "Hello there!";

Well, C# 3.0 was released, and, a new beast arrived in town. Talking about the keyword "var".

Initially, some people thought "var" wold allowed them to use dynamic typing. But it didn't take long to get to know the fact that, "var" was just another form of static typing, which infers it's actual type at compile time. That is,

Once you compile the following lines of codes:

var a = 5;
var s = "Hello there!";
These actually becomes the following:
int a = 5;
string s = "Hello there!";

The logic is pretty simple. The type is inferred from the value which is being assigned to the var variable. In fact, after you write the var statement correctly, Visual Studio Intellisence infers the actual type of the variable from the value.

It's obsious that, you can do the following with normal variables:

int a = 5; //Initializing the int variable with a value 5
And,
int a; //declaring the variable first
a = 5; //Assigning the variable later 

But, you can't do the following with var:

var a; //Will show compile time error
a = 5; 

Why? Because, as soon as compiler encounters the statement "var a", it tries to determine the actual type of "a" (Because there isn't actually type as "var", it's just a place holder type name) by analyzing the value. But, no value is being initialized while declaring the "var", and hence, error message is being shown (Doesn't matter even if you assign a value to the "var" variable later).

Dynamic binding in C# 4.0

Another new beast arrived in town with C# 4.0, the "dynamic" type. Unlike the static bound types, variables declared as dynamic are completely bound at run time. Following is a simple example:

dynamic s = "Hello there!";
Console.WriteLine(s.Length); 

Even if you compile the above statements, CLR doesn't know about the actual type of the variable before actual execution of the statements.

So, Visual studio also is unable to show any Intellisense for dynamic variables. If you take the mouse on the "s.Length" on the above statement, you would see the following Intellisence:

Visual Studio Intellisence for Dynamic types
Figure : Visual Studio Intellisence for Dynamic types
Unlike the "var", it is perfectly legal to declare a dynamic variable and then assign a value to it. That is, following statements are perfectly valid:

dynamic s;
s = "Hello world";
Console.WriteLine(s.Length); 

Why this is possible? Well, for the obvious fact that, dynamic types are resolved at run time, rather than compile time. So, the complier doesn't infer the actual type while compiling the above statements, and, it doesn't complain about any problem.

This gives us the opportunity to use the same variable for different types of data. For example:

dynamic s;

s = "Hello world";
Console.WriteLine(s.Length);

s = 1;
//Console.WriteLine(s.Length); Not supported anymore. s is now an integer now
Console.WriteLine(s); //Outputs 1 

However, unlike PHP, or, JavaScript, dynamic variables in C# are "strongly bound", which means, it is not possible to put two different types of data inside the same dynamic variable at the same time (Probably, using an array). PHP or JavaScript both are "weekly bound" language because they support such flexibility.

dynamic vs object

At first, these two appears to be the same, because, anything could be assigned to a variable of type object. That is, both of the followings are correct:

object o = "Hello there!";
dynamic d = "Hello there!";

But, each of the following statements are not correct:

Console.WriteLine(o.Length);//Compiler error. object class 
//doesn't have a Length property

Console.WriteLine(d.Length);//This is correct, because, at run time
//type of d is determined as string. 

Use of dynamic type give you extreme flexibility in managing the variables and data of your progams, but, these are handy especially in situations when you need to accept and parse data from external sources and you are not sure about the ype of the data.

1 comments:

Anonymous said...

This is an extremely well written article. I've read many articles on the subject, and yours is the first that describes Dynamic variables in a way I can understand them.

I guess the same would apply to dynamic methods. The return value is determined at runtime too. So in this way the same method could return a string or an int.

Post a Comment