Click here to Skip to main content
15,868,065 members
Articles / Programming Languages / C# 4.0

Dynamic Keyword in C# 4.0 - Part 1

Rate me:
Please Sign up or sign in to vote.
3.00/5 (5 votes)
19 Apr 2011CPOL3 min read 20.2K   14   8
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.)

Image 1

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

Image 2

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.

Image 3

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Software Industry
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
johannesnestler10-Apr-14 23:15
johannesnestler10-Apr-14 23:15 
GeneralMy vote of 1 Pin
fjdiewornncalwe19-Apr-11 7:48
professionalfjdiewornncalwe19-Apr-11 7:48 
QuestionDifference from Object Type Pin
d2niraj19-Apr-11 5:41
d2niraj19-Apr-11 5:41 
AnswerRe: Difference from Object Type Pin
Sanjay J Patolia19-Apr-11 5:54
Sanjay J Patolia19-Apr-11 5:54 
GeneralMy vote of 3 Pin
Patrick Kalkman19-Apr-11 4:17
Patrick Kalkman19-Apr-11 4:17 
GeneralRe: My vote of 3 Pin
Sanjay J Patolia19-Apr-11 5:52
Sanjay J Patolia19-Apr-11 5:52 
GeneralMy vote of 2 Pin
Philip Laureano19-Apr-11 3:50
Philip Laureano19-Apr-11 3:50 
Great intro, but I'll give the remaining 3 points if you can expound on the article.
GeneralRe: My vote of 2 Pin
Sanjay J Patolia19-Apr-11 5:50
Sanjay J Patolia19-Apr-11 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.