Rajesh kumar’s Blog

If you have knowledge, let others light their candles at it.

Behavior of dynamic type in C# 4.0

Posted by Rajesh Kumar on June 3, 2009

If you wish to get the type of any object dynamically and then invoke its methods are properties at run time, then C# 4.0 provides a new type called dynamic.

With this dynamic type, we can avoid compile time exceptions for method or property call. Compiler will just ignore dynamic types at the time of compilation. However, they will be evaluated at run time.

During the compile time, the variables of dynamic types are compiled into “object” type. So, type “dynamic” exists only at compile time, but not at run time.

This dynamic type can be used in following scenarios:

  • A private field
  • A public property
  • Method return type
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: