Dynamic feature in C# 4.0

Posted by in C# category on for Beginner level | Views : 7448 red flag

New advancements incorporated in the latest .Net Framework version 4, specifically for C# 4.0


I would like to share with you people the new advancements incorporated in the latest .Net Framework version 4, specifically for C# 4.0
One of the key feature the dynamic which makes most liked programming language a dynamically typed language.

Dynamic Feature

We all are aware that the .Net Framework support strongly typed programming languages. However, it has been found that the type so used are static and at times there is a need to have a type specification that can be dynamic and can be specified later. This involves changing the sizes and other metrics of the such an object. Conventionally we used typecasting to modify type of the data.
Now that there is lot of evolution seen there was a need to improve and increase the scope of the functionality.

How it works

With the advent of dynamic it is not only possible to create dynamic objects and determine their types at run-time but also create expandable objects and advanced class wrappers. Since the dynamic feature enables you work with multiple types it makes interoperability between different languages more convenient.

You will require to use the System.Dynamic namespace if you want to create expandable objects and advanced class wrappers, and provide interoperability between different languages, including dynamic ones.

A simple example of dynamic:

dynamic student = new ExpandoObject();
student.Name = "Deepak Rathod";
student.RollNo = "RB/13/2008/91385/C";

First, look at the declaration of student.
dynamic student= new ExpandoObject();

I didn't write ExpandoObject student= new ExpandoObject(), because if I did student would be a statically-typed object of the ExpandoObject type. And of course, statically-typed variables cannot add members at run time. So I used the new dynamic keyword instead of a type declaration, and since ExpandoObject supports dynamic operations, the code works.

Once I have declared the variable as dynamic I can add more members to it, for e.g.


Now, what if I have to write the name as First Name, Middle Name and Last Name. In that case you have to declare student.Name as an object of ExpandoObject().
student.Name =new ExpandoObject();

Once you are done with this, now you can have the name broken up as

student.FirstName ="Deepak"
student.MiddleName ="Rathod"


The dynamic feature is a key feature of this release of the Framework. However, if you are using the VS2010 IDE you will not find almost no Intellisense for this feature as most of the things are handled at runtime.

I hope you all gained some knowledge from this article.

Do rate this article and let know if you have any questions.


For more details on this topic, please visit 

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Shailesh Pisat
Member Level: Starter
Member Status: Member
Member Since: 9/17/2009 1:20:14 PM

I am about to complete my ACCP course from Aptech.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)