One of the new things being introduced with .net 4.0 is the dynamic keyword. And it serves as a good entry point to calling dynamic languages such as iron python and iron ruby or situations where a static approach is troublesome.
I’m basically more of static language guy than a dynamic language guy. But I must admit if used for good the dynamic features has some potential.
I’m planning a post where I’ll show an example of using it for reading values from f.ex a SqlDataReader in a very easy fashion. This post serves as a prelude to that one. With a short introduction to the dynamic keyword.
The dynamic keyword
Before i continue lets shortly examine what the dynamic keyword is with this short and rude example.
public class YoungChildClass
{
public string DoSomethingEvil()
{
return "Bully a child";
}
}
public struct SithClass
{
public string DoSomethingEvil()
{
return "Kill jedis";
}
}
Here we have two classes simulating two kind of persons, for whom evil comes naturally. A YoungChildClass and a SithClass (Shame on you).
They are not connected through any interface (IEvilDoer) or inheritance (BaseOfEvil) though. So if you wanted you couldn’t immediately create a shared method that would call the DoSomethingEvil method without using some kind of reflection or spliting it up:
public static void DoEvilThing(YoungChildClass knownEvilDoer)
{
Console.WriteLine(knwonEvilDoer.DoSomethingEvil());
}
public static void DoEvilThing(SithClass knownEvilDoer)
{
Console.WriteLine(knwonEvilDoer.DoSomethingEvil());
}
In comes the dynamic keyword that lets you do that very easily. I can instantiate my evilDoer as dynamic. When I’m using the dynamic keyword I can call whatever method I want to.
static void Main(string[] args)
{
dynamic evilDoer = new YoungChildClass();
Console.WriteLine(evilDoer.DoSomethingEvil());
DoEvil(new SithClass());
}
private static void DoEvil(dynamic evilDoer)
{
Console.WriteLine(evilDoer.DoSomethingEvil());
}
I can initialize the YoungChildClass as a dynamic and call DoSomethingEvil(). Or pass any arbitrary object to the DoEvil method.
I could even go apeshit insane and call:
evilDoer.MakeBakedPotatoesAndStuffedBananas()
And the compiler wouldn’t complain. Because the evilDoer object is declared dynamic, so it won’t check if the actual method is there at compile. But if i ran the program it would fail because none of the classes have the insanity method on them.

But look at all the information you get (besides the insight in the programmers mental state). This is not an Object Reference not set to an instance of an object (I think I’ll dedicate a complete blog post ranting about uninformative exceptions).
Another thing you don’t get is intellisense. The object is dynamic so even though I’ve initialized a known object (YoungChildClass) I have to know which method to call. If you look at the DoEvil method this will make more sense.
Is this basically a prettier way to do magic strings?
Hell yeah! But it is also much more than that. As i mentioned in the introduction to this blog post, It is something that can be used when calling dynamic languages. I really haven’t played around with that part very much, so I won’t dig into that in this post. But without dynamic you would have to write code that could be really nasty to look at.
And that brings me forward to one of the major advantages of the dynamic keyword. It can make calling non static typed object much prettier and easier. Especially the scenarios that would involve a lot like this:
int? result = reader["Years"].Value == DBNull.Value ? null : reader["Years"].Value;
It Can be solved much prettier. But that is the topic of the upcoming blog post. This will also provide a more concrete example of dynamic, compared to this more or less obscure example. (I myself prefer concrete examples)
PS. The dynamic keyword is just the tip of the iceberg. So go look around the net and learn more about it. You will most likely come across that strangely named class ExpandoObject.
May the force be with you!