Dynamic Typing Is Contagious

Starting from C# 4.0, dynamic is one of the types built in to the language in the form of a keyword. Everywhere other types can be used, you can use dynamic, too. For example, variables can be declared to be dynamic, and they can appear as types for members and their parameters. Yes, even generic parameters can be using dynamic.

One of the first things we should look at is the effect of using some dynamically typed expression in a larger expression. To make this more concrete, look at the following example. A multiplication of a dynamically typed object with a statically typed object is made:

dynamic a = 3;int b = 5;var c = a * b;

The obvious question is what the inferred static type for c will be. Recall that all ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.