If you had a chance to take a look at the upcoming edition of .NET and C# you probably read about a keyword introduced called “dynamic”. So what this is all about? Using the dynamic keyword there is type resolution taking place during runtime and not during compile.

For example the next code snippets executes just fine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DynamicTest
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic x = "Test";
            dynamic f = 0;
            Console.WriteLine(f);
            Console.WriteLine(x);
            f = "0";
            Console.WriteLine(f);
 
            Console.Read();
        }
    }
}

As you noticed first I use my variable, declared as dynamic, as an integer but afterwards as a string.

In case I want to get a list of the available methods/properties etc on my variable (Intellisense in other words) Visual Studio IDE is warning that type resolution is taking place during runtime, so any method that actually exists it will be called fine. Take a look at this:

f += 1

and also

f = f.Replace("0", "1");

are going to execute without a glitch but only when they are called at the right place. If you call it a little earlier (before it’s “converted” to a new type where the method you want exists) you get an “RunTimeBinder exception” during… runtime.

If we use generics, we don’t actually achieve runtime type resolution because everything is being boxed to Object and not their “real” type (string, int, bool etc). Dynamic keyword performance can be easily compared with generics as it’s slightly slower.

Oh, and if you ask why not “var”? Well, var is compile-time resolution and it can’t be used as “dynamic”.