Software Architecture – C# 4.0 – New Feature – Dynamic type

C# 4.0 has introduced a new concept called dynamic type.
In order to understand this concept, it is important to know
• The difference between static and dynamic languages.
• The difference between strong and week typed languages.

After some googling, I have finalized some definitions as:
In Static languages, the type binding of the variable to the type is done at compile time. So all their variables must be defined to be of a certain type at compile time. Examples: C++,Java, C#

In dynamic languages, the type binding is done at interpretation/run-time. At compile time, the type of a variable is not specified. Instead it is bound to be of a certain type in run time. Examples are PHP, Javascript. (Mostly interpreted languages)

Static typing is more efficient and reliable, but loses power.

In Strong typed language, type checking rules like type determination, type mixing and type conversion rules are strictly followed, otherwise a exception is thrown. Examples Java, C#
In weekly typed languages, type checking rules are not strictly followed. So you can use one type for other. Example PHP, Javascript.

So in short, Staic/Dynamic deals “When” of type checking for a language, however Strong/Week deals with “How” of type checking for a language.

From discussion above, it is clear that C# is Static and strongly typed language. Considering the benefits of dynamic nature of languages, Microsoft has introduced some dynamic aspect of programming in C# by introducing a new type called “dynamic”. As the name suggests, type determination of “dynamic” type can be delayed till run time. So with feature C# has become a static language with some dynamic features in it.

Examples:

   int i1 = 5;
            string s1 = " Five ";
            dynamic d1;
            d1 = 5;         //  dynamic type variable can be assignbed any value .
            d1 = " Five ";  //  dynamic type variable can be assignbed any value .
            d1 = i1;        //  dynamic type variable can be assignbed any type variable .   
            d1 = s1;        //  dynamic type variable can be assignbed any type varaible.
            i1 = d1;        //  No Compile time error, but a run time error as "Cannot implicitly convert type 'string' to 'int'" as in last statement d1 type was converted to string type
            s1 = d1;        // No Error because with statement 'd1 = s1;' d1 type was changed to string type, so it can be assigned to other string type variables like s1

Consider other case as:

int i1 = 5;
            string s1 = " Five ";
            dynamic d1;
            d1 = s1;        //  dynamic type variable can be assignbed any type varaible.
            d1 = i1;        //  dynamic type variable can be assignbed any type variable .   
            s1 = d1;        //  No Compile time error, but a run time error as "Cannot implicitly convert type 'int' to 'string'" as in last statement d1 type variable was converted to int type 
            i1 = d1;        // No Error because with statement 'd1 = i1;' d1 type was changed to int type, so it can be assigned to other int type variables like i1

References:
http://msdn.microsoft.com/en-us/library/dd233052.aspx

Advertisements

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

%d bloggers like this: