C# 4.0 and the new "dynamic" keyword

I was searching a bit the new C# 4.0 features revealed in these days and the main thing that gives me a little of surprise and doubts is the new dynamic keyword.

This new keyword allows you to declare an object whose method calls will be resolved at runtime. This is extremely different from the normal C# behaviour (static typing): actually when you call a method or a property of an object, the compiler checks if it exists and, if not, it raises a runtime error.

With dynamic typing, you can write something like this:

dynamic MyInt = 1;
dynamic MyString = "Hello world!";
Console.WriteLine(MyInt);
Console.WriteLine(MyString);

It seems to me like a VAR declaration but with a runtime resolution...

Dynamic typing is certainly not a news in the world of computer languages (Python is an example of good dynamically-typed language), but I'm thinking about what could be the real advantages of this feature... maybe interoperability with other dynamically-typed languages?

I can agree that dynamic typing could be a panacea for certain scenario, but I'm a big fan of the old and elegant compile-type check and object casting. I also think (but maybe I'm wrong) that runtime bug detection (instead at compile time) could be a problem.

And what about the performance overhead of declaring a dynamic object variable?

I think that someone will solve my doubts soon...

Technorati Tags:

Print | posted on Friday, October 31, 2008 10:03 PM

Comments on this post

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
you should watch the PDC videos, they answer all your questions

http://www.microsoftpdc.com/
Left by Eber Irigoyen on Oct 31, 2008 10:37 PM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
Can you point me to the right video?
Left by Stefano Demiliani on Oct 31, 2008 10:54 PM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
Miguel's explanation:
1) "object x" is a shorthand for "System.Object x". This declares the variable x to have the type System.Object, this is strongly typed. And since C# provides autoboxing, you can assign anything you want to this variable.
2) "var x = E" declares a variable x to be of the type of the expression E. The E is required, not optional. This is a strongly typed declaration, and you can only assign values whose type is typeof(E) to it.
3) "dynamic x" declares the variable x to have dynamic semantics. This means that the C# compiler will generate code that will allow dynamic invocations on x. The actual meaning of "x.M" is deferred until runtime and will depend on the semantics of the IDynamicObject implementation.
Left by Stefano Demiliani on Nov 05, 2008 10:05 AM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
you should watch the PDC videos, they answer all your questions
Left by watches replicas on Apr 29, 2010 3:16 AM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
The script search on every database on a server and remove the login's <h2>Links of London Gold Fringe Earrings</h2>permissions and object ownership.<h2>Hermes Classic H Necklace-Blue</h2> When a login you want to remove owns a database, that database ownership is given to
Left by jewellery earrings on May 11, 2010 8:57 AM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
allows you to declare an object whose method calls will be resolved at runtime. This is extremely different from the normal C# behaviour (static typing): actually when you call a method or a property of an object, the compiler checks if it exists and, if not, it raises a runtime error.

With dynamic typing, you can write somethi notebook battery supplier and notebook adapter supplier
Left by wholesale laptop adapter on May 23, 2010 2:27 PM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
I was looking for some blog to discuss NHibernate and it seems that it is a right place for that. Thank you so much for that
Left by superb on Jun 01, 2010 11:16 AM

# re: C# 4.0 and the new "dynamic" keyword

Requesting Gravatar...
It seems to me like a VAR declaration but with a runtime resolution...
Left by han on May 27, 2012 12:41 PM

Your comment:

 (will show your gravatar)
 
Please add 1 and 2 and type the answer here: