Dynamic Programming Using C# 4.0 and Microsoft Visual Studio 2010

Introduction

C# 4.0 introduces flexible and declarative style of programming by using the dynamic language runtime library (DLR). This style is called dynamic programming. Dynamic programming might not be new to you if you are a C++, Python or Ruby developer. Programmers can now use the 'Dynamic' keyword (the new static type) for creating typed objects or placeholders. These dynamic type objects bypass compile-time type checking that's why types of these placeholders are unknown until runtime. In .NET framework 4.0 architecture DLR is built on top of CLR (common language runtime) to support dynamism of dynamic programming languages like Python and Ruby . In this article I will show how you can create dynamic objects using the 'Dynamic' keyword.

How to Use Dynamic

Let me first show you some code snipped from a dynamic type declaration. You just need to declare a variable whose static type is dynamic. As I have already discussed above, dynamic is typed objects or placeholders (like var in Visual Basic) that indicates the compiler that object type will be resolved dynamically.

The above code will work in compile time but will throw an exception in runtime as the 2nd statement is trying to access type of null reference. Dynamic objects can't perform object binding on null reference.

The above code will throw an implicit conversion error in runtime (string can't be converted to integer). Dynamic will also throw runtime exceptions for non existing property or method. The following code snippet will throw RuntimeBindingException for the same reason.

Now let me show you a working example dynamic keyword. For creating the sample C# programming console application I have used Microsoft Visual Studio 2010 Ultimate Edition with .NET framework 4.0. First I have created a currency conversion class with 1 method named ConvertCurrency(). This ConvertCurrency() method converts source currency to destination currency type using a conversion factor. The following is a code snippet of the CurrencyConvert class.

Next I have written the main() method to show how both static CurrencyConvert class object and dynamic object works. For both the cases you will get an identical converted value if you are passing the same input parameters. Main() method code example is shown below:

Dynamic and Overloading of Methods

In case of overloaded methods; runtime resolves which function needs to be called based on input parameter data type. To demonstrate the runtime resolution of dynamic objects I have created a calculator class with 2 overloaded versions of add method. Following is the code snippet from my calculator class.

Conclusion

Using dynamics, developers can now make interop calls. You can also call COM object's method using dynamic. Dynamic keyword can be used to replace complicated reflection code you have already written but there are also a few limitations. For example, you can't call extension methods and anonymous functions.

Top White Papers and Webcasts

This white paper examines the economics of deploying Red Hat's Storage Server. Based on GlusterFS, a distributed file system that Red Hat acquired as part of Gluster, Red Hat Storage Server is ushering in a new era of software-based storage (also known as software-defined storage by many suppliers) solutions. Such solutions leverage commodity x86-based hardware from server vendors and a distributed shared nothing architecture that allows businesses to build out a service-based storage infrastructure in an …

Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …