Dynamic feature in C# 4.0


Dynamic feature in 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.

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.

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.

student.Email="drathod@abc.com";


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.


Reference: http://softwarecrafter.blogspot.com


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: