Information Technology Reference
In-Depth Information
// Type arguments needed because
// args are not the same type
answer2 = Add< int , double , double >( 5 , 12.3 );
Console .WriteLine(answer);
string stringLabel = System. Convert .ToString(answer);
string label = Add( "Here is " , "a label" );
Console .WriteLine(label);
DateTime tomorrow = Add( DateTime .Now, TimeSpan .FromDays( 1 ));
Console .WriteLine(tomorrow);
label = "something" + 3 ;
Console .WriteLine(label);
label = Add( "something" , 3 );
Console .WriteLine(label);
The above code is the same example from Item 38. Notice that this version
has static types that are not dynamic as the return values. That means the
caller does not need to work with dynamically typed objects. The caller
works with static types, safely ignoring the machinations you needed to
perform to make the operation work. In fact, they don't need to know that
your algorithm ever left the safety of the type system.
Throughout the samples in this chapter, you saw that dynamic types are
kept isolated to the smallest scope possible. When the code needs to use
dynamic features, the samples show a local variable that is dynamic. The
methods would convert that dynamic object into a strongly typed object
and the dynamic object never left the scope of the method. When you use
a dynamic object to implement an algorithm, you can avoid having that
dynamic object be part of your interface. Other times, the very nature of
the problem requires that a dynamic object be part of the interface. That is
still not an excuse to make everything dynamic. Only the members that
rely on dynamic behavior should use dynamic objects. You can mix
dynamic and static typing in the same API. You want to create code that is
statically typed when you can. Use dynamic only when you must.
We a l l h a v e t o w o r k w i t h C S V d a t a i n d i f f e r e n t f o r m s . Re a d i n g a n d p a r s -
ing the CSV data is a relatively simple exercise, but a general solution is
almost always lacking. This snippet of code reads two different CSV files
with different headers and displays the items in each row:
 
Search WWH ::




Custom Search