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.
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)
Hello, Guru //Comment: Output
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)
>>> addTwo("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.