Dynamic Keyword in C# 4.0 - Part 1

Introduction

It is a new feature in C# 4.0 - programming in the language where I don’t know anything until run time whether that code will succeed well. Dynamic objects type may not be known until runtime and that means the members of that dynamic object and the signatures will not be known at runtime. It means that the calls to the methods of that dynamic object will be dispatched dynamically. When we say dynamically dispatched, it may fail if it does not find a given method at run time. It bypasses compile time type checking so dynamic operations will be resolved at runtime.

How to Use

We use a keyword dynamic to create dynamic operations and use it like any other type.

As the above figure says how to start using dynamic keyword

Make a return type of method as dynamic type.

Pass parameter of dynamic type, to declare a variable of dynamic type where value is of an integer type.

Declare a variable of dynamic type where value is of a string type.

Declare a variable of dynamic type where value is of a floating type.

Declare a variable of dynamic type where value is of a reference type.

When we say a reference type, we come across the members of reference type such as variables methods, etc. Those members will be resolved at run time. It does not give any compilation error if we give a method name which does not exist. In this case, it generates a run time binder error.

Compiler Behavior

As we discussed, it bypasses compile time type checking. Instead, at compile time, it records metadata about the various calls such as what it means, what are the types of parameters passed in. So that the metadata can be used during run time to resolve the call and one of these two things is going to happen; either it gets dynamically dispatched or it generates a run time error.

Compile Time Members of Dynamic Type Variable

As the above code (underlined in red) says, we tried to access the members of a dynamic type variable named “dynamicVariableInt” and it shows that “This operation will be resolved at runtime”.

Now, you would have the questions about what happens if I forcefully give the member names. Refer to the below code and read comments to see what happens, when we forcefully give the member names.

As we read that dynamic objects get resolved at run time, the second last statement in the above code does not generate a compile time error because the whole operation will be resolved at run time. So it throws an error (Run time binder exception) at run time.

At run time, it looks for member name implemented and does much more with the classes of System.Core -> System.Dynamic namespace.

Comments and Discussions

Good infos, maybe add some real world examples for usage and show the "default" error handling code you should apply to every dynamic usage (handle RuntimeBinderException). Could be a tip, but you said you want to create a series... thank you for sharing!

This is not an article. It is very much incomplete. I would suggest that all 3 of the articles you have presented should be combined into one article. As it stands right now, this isn't very good at all, a tip at best.

Thanks for your interest. Sure I will expand. This was just to make aware and understand a keyword so that coming articles will be easily understandable. I have already submitted part 2 of this and will be available soon. Thanks

Thanks for your interest. Sure I will expand. This was just to make aware and understand a keyword so that coming articles will be easily understandable. I have already submitted part 2 of this and will be available soon. Please keep checking this space. Thanks