Click here to Skip to main content
Click here to Skip to main content

Dynamic Keyword in C# 4.0 - Part 1

By , 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.

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, 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

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberjohannesnestler10-Apr-14 23:15 
GeneralMy vote of 1 PinmemberMarcus Kramer19-Apr-11 7:48 
QuestionDifference from Object Type Pinmemberd2niraj19-Apr-11 5:41 
AnswerRe: Difference from Object Type PinmemberPatolia Sanjay19-Apr-11 5:54 
GeneralMy vote of 3 PinmemberPatrick Kalkman19-Apr-11 4:17 
GeneralRe: My vote of 3 PinmemberPatolia Sanjay19-Apr-11 5:52 
GeneralMy vote of 2 PinmemberPhilip Laureano19-Apr-11 3:50 
GeneralRe: My vote of 2 PinmemberPatolia Sanjay19-Apr-11 5:50 

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

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

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