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


Related Articles

SilverPop Integration

SilverPop is a third party emailing tool which is not only provide the facility to prepare the email templates which needs to be used in sending an email as well as it provides the support for campaign management through various things.

ASP.NET 4 Brings Clean Web.config Files

Major configuration elements have been moved to the machine.config file in .NET 4 With the advent of the latest iteration of Microsoft's development platform and tools, the company took a number of steps designed to result in optimized desktop and Cloud applications for devs, simply by leveraging Visual Studio 2010 and .NET Framework 4. An illustrative example in this regard is the new Web.config file that can accompany ASP.NET 4.

More articles: VS 2010 features C# 4.0 ASP.Net 4.0 Visual Studio

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: