Introduction
This post speaks about the future of C# which presented by Anders Hejlsberg chief architect of C# at PDC 2008. The coming version C#4.0 is much concerned on introducing Functional and Dynamic programming language concepts into the C#. It also speaks about C#4.0 features.
The factors that shape the C#4 are
- Declarative programming
- Dynamic programming
- Concurrence programming
Declarative programming
Imperative programming style makes programming tedious and very hard for execution environment to execute the statements efficiently.
Example Let say we are writing a program for sorting some data in memory by looping up in dictionary and setting a for loop for completing the logic. By the time this program compiles the JIT compiler has no choice it has to execute the statements step wise as we wrote. There is no way for JIT compiler to automatically paralyze the execution of the program.
Instead if you write the program in higher level example in LINQ then there is a better chance for JIT compiler to execute them in parallel.
Dynamic programming
It introducing the features like in dynamic languages [ruby,python] such as implicit typing and meta-programming.
Concurrency
Writing the programming in multiple units that can execute parallel in multi core
environment.
C# 4.0 Language Features
- Dynamically Typed Objects
- Optional and Named Parameters
- Improved COM Interoperability
- Co- and Contra-variance
.NET Dynamic Programming
DLR Dynamic Language Runtime is a technology which unifies the Dynamic Programming on .NET platform. It is an extension of CLR and it on top of CLR. It has the core capabilities like expression tress, Dynamic Dispatch and Call Site Caching.
It has Object Binders to talk with technologies .NET,silver light,python and ruby.
DLR giving you single programming experience by allowing you to talk with all other environments shown in the above figure.
Dynamically Typed Objects
The above code snippet is having Calculator class which is statically typed one. We have a method GetCalculator() which returns of the type Class Calculator. It is having add method which produces the SUM of two numbers.
Now let say that Calculator class which not written in .NET and it is in some programming language like ruby,python or assume it is a COM class.
If the Calculator class is written in COM then we need to write the following code to use the class
If the Calculator written in Java script then we may need to write the following code to use the class.
There is no unification in programming and we writing different syntaxes for getting the same result.
With C# 4.0 it possible to write the following code for unification of the programming.
The dynamic works like
anything can be assigned to dynamic as shown in below
You can declare variables of type dynamic and it is just like object and at runtime it determines the actual type assigned to it. Similarly the result is also of type dynamic.
Let us take a practical example to explain how dynamic works
We are looking Math class in System which contains an overload method absolute for variety of types.
In statically type code we might write the following code to get the result from Math class
We can write the same code above in dynamic programming using C# as follows
It resolute the method at runtime.
In the next article I will explain the other features of C# 4.0.
Mmmm… I’m afraid that this feature may lead to developers declaring each variable as dynamic, by lazyness…
That’s what happens already in Javascript…