Information Technology Reference
In-Depth Information
5
Dynamic Programming in C#
There are advantages to both static typing and dynamic typing. Dynamic
typing can enable quicker development times and easier interoperability
with dissimilar systems. Static typing enables the compiler to find classes
of errors. Because the compiler can make those checks, runtime checks
can be streamlined, which results in better performance. C# is a statically
typed language and will remain one. However, for those times when
dynamic languages provide more efficient solutions, C# now contains
dynamic features. Those features enable you to switch between static typ-
ing and dynamic typing when the needs arise. The wealth of features that
you have in static typing means that most of your C# code will be statically
typed. This chapter shows you the problems suited for dynamic pro-
gramming and the techniques you will use to solve those problems most
efficiently.
Item 38: Understand the Pros and Cons of Dynamic
C#'s support for dynamic typing is meant to provide a bridge to other
locations. It's not meant to encourage general dynamic language pro-
gramming, but rather to provide a smoother transition between the
strong, static typing associated with C# and those environments that use
a dynamic typing model.
However, that doesn't mean you should restrict your use of dynamic to
interoperating with other environments. C# types can be coerced into
dynamic objects and treated as dynamic objects. Like everything else in
this world, there's good and bad in treating C# objects as dynamic objects.
Let's look at one example and go over what happens, both good and bad.
One of the limitations of C# generics is that in order to access methods
beyond those defined in System.Object, you need to specify constraints.
Furthermore, constraints must be in the form of a base class, a set of inter-
faces, or the special constraints for reference type, value type, and the exis-
tence of a public parameterless constructor. You can't specify that some
227
 
 
 
 
Search WWH ::




Custom Search