TOC
Data types:

The dynamic Type

In C# version 4.0, Microsoft introduced a new type: the dynamic. It's actually a static type, but unlike other static types, the (potential) members of a dynamic object is not checked by the compiler.This will give you some of the advantages of the dynamically/weakly typed languages, while maintaining the advantages of a strongly typed language in all other cases.

Declaring a dynamic is like declaring any other type - simply use the dynamic keyword instead of the data type:

dynamic d1;
dynamic d2 = "A string";
dynamic d3 = 42;

Now you have three different objects - the first is really nothing at this point (null), while the second is a string and the third an integer. The interpreter will automatically decide this at runtime, based on what you assign to the variables. That also means that the compiler won't check up on what you do with these variables, as illustrated by this next example:

dynamic d1;
dynamic d2 = "A string";
dynamic d3 = 42;
Console.WriteLine(d2.Length);
Console.WriteLine(d3.Length);

Strings have a length property, to decide how long the string is, but an integer doesn't. In this case, I try to use this property on both variables, and the compiler won't complain about it - if these variables had been declared as a string and an integer, the compiler would have intercepted. But since the types are dynamic, it just compiles and run. However, as soon as the interpreter reaches the last line, an exception will be thrown, because obviously you can't access a property which doesn't exist.

This also shows the danger of the dynamic types - you have to be aware of what you're doing and make sure that you spell every property and method call properly, because the compiler won't double check your work, basically allowing you to publish code which fails every time.

A dynamic object

The dynamic keyword can, of course, be used for more complex types than integers and strings. A great example of this is when it's used to hold an anonymous object, like this:

dynamic user = new
{
    Name = "John Doe",
    Age = 42
};
Console.WriteLine(user.Name + " is " + user.Age + " years old");

This allows you to create an object without defining a class for it first. The dynamic keyword can be used to hold it, but so can the var keyword, which might be better suited in a lot of situations.

You might think that since the type is dynamic, you can just add properties to it later on, like this:

dynamic user = new
{
    Name = "John Doe",
    Age = 42
};
user.HomeTown = "New York";

Since no compile-time checking is done on a dynamic type, this code actually runs, because the compiler doesn't actually validate the existence of the HomeTown property, but as soon as the last line is reached, an exception will be thrown. While a dynamic type IS very dynamic, it doesn't allow you to dynamically add new properties to it. For that, you can use an ExpandoObject, which we'll discuss in the next article.

Changing type

A variable declared as a dynamic is not typeless at all. Instead, C# will treat it internally as whatever kind of object you have assigned to it. In our very first example, you will see how we can declare a variable as a dynamic, assign either a string or an integer to it, and then start using properties belonging to these types. You can, however, easily change the type of a dynamic variable - just assign a new value to it. Here's an example:

dynamic user = new
{
    Name = "John Doe",
    Age = 42
};
Console.WriteLine(user.GetType() + ": " + user.Name + " is " + user.Age + " years old");
user = "Jane Doe";
Console.WriteLine(user.GetType() + ": String.Length = " + user.Length);

Notice how we use the dynamic user variable: First it holds an anonymous object and then we assign a simple string to it instead. We validate it by using the GetType() method found on all C# objects, and in both cases, we access properties found on the type we currently have (first Name/Age properties of the anonymous object, then the Length property found on string objects).

Summary

The dynamic keyword allows you to use variables which are not type-specific - instead, they act as the kind of data which they hold. You can easily assign a new value to a dynamic variable, and if the type of value you are assigning is not the same as it currently holds, the dynamic variable will simply change type automatically.

Dynamic variables are not checked by the compiler, allowing you to access properties which may or may not be present. This makes them very flexible, but it also makes your applications a lot more vulnerable to errors. Especially for this reason, you may want to restrict the usage of the dynamic keywords to situations where you can't accomplish what you want without using it. Examples of when it makes good sense to use the dynamic keyword are for COM interop and when interacting with data formats like JSON and XML.