Abstract Confusions

Complexity is not a cause of confusion. It is a result of it.

Dynamic Typing – New Feature in C# 4.0

Microsoft has released a new version of its flagship product .NET. Visual Studio is highly productive environment for professional to develop and deploy .NET frame work applications. But it still lacked few features which if present would have been better. One of those features is ‘dynamic programming or dynamic typing’. A new version Visual Studio 2010 is released recently. C# 4.0 has this feature.

Dynamic Typing / Dynamic Programming

Dynamic typing as it is known famously has more advantages over static typing. More often people note that, in a statically typed language, a variable – the name, is bound to an object and also bound to a data type. The data type a variable takes needs to be known at the compile time itself. It is true in Java, C and C++.

In a dynamically typed language, the variable, the name is bound to only object. The data type is not declared up front. All happens in runtime. Pretty much true with programming language Python. Now, with Visual studio 2010 release, we will have C# 4.0. C# 4.0 also has dynamic typing feature now.Dynamic Typing in C# 4.0

The new version C# 4.0 has dynamic typing feature. It is achieved by declaring a variable using ‘dynamic’ keyword. You can download express edition of Visual C# 4.0 and try it yourself.

dynamic aVarName = Console.Readline();

A sample program with dynamic typing.



namespace sayHelloConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic a = "Hello";
            dynamic b = Console.ReadLine();
            Console.WriteLine("Hello, " + b);
            dynamic c = Console.ReadLine();
            dynamic d = Console.ReadLine();
            Console.WriteLine(Convert.ToInt32(c) + Convert.ToInt32(d));
        }
    }
}

Executing it, you can see (see comments, they are the output line)


C:\Users\guru>sayHelloConsole
Guru
Hello, Guru     //Comment: Output
12
23
35              //Comment: Output

The dynamic future is explained pretty neat in here. Check the Microsoft Visual Studio 2010 sample code page for C# 4.0 new features.

The Python Way

The same could be done in Python this way..

>>> def addTwo(a, b):
...    return a + b
...
>>> addTwo(12, 23)
35
>>> addTwo("Hello, ", "Guru")
'Hello, Guru'

Pretty elegant, I would say. See, the adding two objects implementation did not change for integers and string. In C# 4.0, I have to use Convert.ToInt32 for adding numbers. Either I don't know how to do this in C# 4.0 or I could not find. If you know, add it in comments, I will amend the post.

5 responses to “Dynamic Typing – New Feature in C# 4.0

  1. Pingback: Most important missing feature in new iPhone – QuickPWN (blog) | Ipad Library Store

  2. Pingback: Dynamic Typing – New Feature in C# 4.0 « Abstract Confusions | Source code bank

  3. Karthikeyan April 26, 2010 at 4:41 AM

    Guru…
    These perky features from scripting are really handy when it comes along with programming languages. Eventually writing compilers will get complex day by day🙂

    • SREE GURUPARAN April 26, 2010 at 9:12 AM

      “Dynamic typing”- I would say is really one hell of a feature. And who will complain if you can write once and use it all over.

      I am not going to worry about compilers (it is job of people who write it, perhaps, for people LIKE you😉 )

  4. Kenneth Thomason May 27, 2010 at 2:01 PM

    Super great post! Really..

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: