CHAPTER 16 DYNAMIC TYPES AND THE DYNAMIC LANGUAGE RUNTIME
Console.WriteLine(textData1.ToUpper());
}
// You would expect compiler errors here!
// But they compile just fine.
Console.WriteLine(textData1.toupper());
Console.WriteLine(textData1.Foo(10, "ee", DateTime.Now));
Notice the second call to WriteLine() attempts to call a method named toupper() on the dynamic
data point. As you can see, textData1 is of type string and, therefore, you know it does not have a
method of this name in all lowercase letters. Furthermore, string certainly does not have a method
named Foo() that takes an int, string, and DataTime object!
Nevertheless, the C# compiler is satisfied. However, if you invoke this method from within Main(),
you will get runtime errors similar to the following output:
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
'string' does not contain a definition for 'toupper'
Another very big distinction between calling members on dynamic data and strongly typed data is
that when you apply the dot operator to a piece of dynamic data, you will not see the expected Visual
Studio IntelliSense. Instead, you will find the general message shown in Figure 16-1.
Figure 16-1. Dynamic data will not activate IntelliSense
It should make sense that IntelliSense is not possible with dynamic data. However, remember that
this means you need to be extremely careful when you are typing C# code on such data points. Any
misspelling or incorrect capitalization of a member will throw a runtime error, specifically an instance of
the RuntimeBinderException class.
602