Click here to Skip to main content
13,087,941 members (68,124 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 19 Apr 2011

Dynamic Keyword in C# 4.0 - Part 1

, 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.
An introductory article about the dynamic keyword in C# 4.0


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


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.


  • 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


  • 19th April, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sanjay J Patolia
Software Developer Software Industry
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
johannesnestler10-Apr-14 23:15
memberjohannesnestler10-Apr-14 23:15 
GeneralMy vote of 1 Pin
Marcus Kramer19-Apr-11 7:48
memberMarcus Kramer19-Apr-11 7:48 
QuestionDifference from Object Type Pin
d2niraj19-Apr-11 5:41
memberd2niraj19-Apr-11 5:41 
AnswerRe: Difference from Object Type Pin
Patolia Sanjay19-Apr-11 5:54
memberPatolia Sanjay19-Apr-11 5:54 
GeneralMy vote of 3 Pin
Patrick Kalkman19-Apr-11 4:17
memberPatrick Kalkman19-Apr-11 4:17 
GeneralRe: My vote of 3 Pin
Patolia Sanjay19-Apr-11 5:52
memberPatolia Sanjay19-Apr-11 5:52 
GeneralMy vote of 2 Pin
Philip Laureano19-Apr-11 3:50
memberPhilip Laureano19-Apr-11 3:50 
GeneralRe: My vote of 2 Pin
Patolia Sanjay19-Apr-11 5:50
memberPatolia Sanjay19-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Sanjay J Patolia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid