Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamic Keyword in C# 4.0 - Part 1

0.00/5 (No votes)
19 Apr 2011 1  
An introductory article about the dynamic keyword in C# 4.0

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.

Implementation of dynamic happens using the component DLR (Dynamic Language Runtime).

How to Use

(DO NOT CARE WHAT TO ASSIGN.)

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.

Limitations

  • Dynamic look up can’t resolve extension methods
  • Dynamic method call can’t pass anonymous functions
  • Can’t use LINQ over dynamic objects

DLR (Dynamic Language Runtime)

  • All the operations happen under dynamic lookup based on component DLR
  • It is a normal assembly hosted on CLR (System.Core)
  • Uses lightweight code gen part of System.Reflection namespace

History

  • 19th April, 2011: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here