Introduction
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, silverlight, 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.