Older versions of C# had trouble interacting with certain kinds of programs, especially those in the Microsoft Office family. You could get the job done, but before C# 4.0, it needed a lot of effort, and the results were ugly. The problem came down to a clash of philosophies: Office embraces a dynamic style, while C# used to lean heavily toward the static style. C# 4.0 now provides better support for the dynamic style, making it much easier to program Microsoft Office and similar systems from C#.
What exactly is the difference between static and dynamic?
The terminology is slightly confusing because C# has a keyword called
static which is
unrelated, so you’ll need to put your knowledge of that
static to one side for now. When it comes to the
something is dynamic if it is decided at runtime, whereas a static feature
is determined at compile type. If that sounds rather abstract, it’s
because the distinction can apply to lots of different things, including
the choice of which method to call, the type of a variable or an
expression, or the meaning of an operator.
Let’s look at some concrete examples. The compiler is able to work out quite a lot of things about code during compilation, even code as simple as Example 18-1.
Example 18-1. Simple code with various static features
var myString = Console.ReadLine(); var modifiedString = myString.Replace("color", "colour");
We’ve used the
var keyword here, so we’ve not told the ...