13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Type system characteristics47public Person Clone(){}[Implementation goes here]object ICloneable.Clone(){return Clone();}Calls non<strong>in</strong>terfacemethodImplements<strong>in</strong>terface explicitlyAny code that calls Clone() on an expression that the compiler knows is of type Personwill call the top method; if the type of the expression is just ICloneable, it will call thebottom method. This works but is really ugly.The mirror image of this situation also occurs with parameters, where if you hadan <strong>in</strong>terface or virtual method with a signature of, say void Process(str<strong>in</strong>g x), thenit would seem logical to be able to implement or override the method with a lessdemand<strong>in</strong>g signature, such as void Process(object x). This is called parameter typecontravariance—and is just as unsupported as return type covariance, with the sameworkaround for <strong>in</strong>terfaces and normal overload<strong>in</strong>g for virtual methods. It’s not ashowstopper, but it’s irritat<strong>in</strong>g.Of course, <strong>C#</strong> 1 developers put up with all of these issues for a long time—and Javadevelopers had a similar situation for far longer. While compile-time type safety is agreat feature <strong>in</strong> general, I can’t remember see<strong>in</strong>g many bugs where people actually putthe wrong type of element <strong>in</strong> a collection. I can live with the workaround for the lackof covariance and contravariance. But there’s such a th<strong>in</strong>g as elegance and mak<strong>in</strong>gyour code clearly express what you mean, preferably without need<strong>in</strong>g explanatorycomments. We’ll see later that <strong>C#</strong> 2 isn’t flawless either, but it makes large improvements.As an aside, let’s briefly go <strong>in</strong>to an area that isn’t improved upon <strong>in</strong> any way by<strong>C#</strong> 2 and is only tangentially touched on by <strong>C#</strong> 3—dynamic typ<strong>in</strong>g.2.2.3 When does <strong>C#</strong> 1’s type system get <strong>in</strong> the way?I like static typ<strong>in</strong>g. Most of the time, it does what I want without much fuss, and it stopsme from mak<strong>in</strong>g silly mistakes. It also means the IDE has more <strong>in</strong>formation to help mewith features such as IntelliSense.Just occasionally, it’s a real pa<strong>in</strong>. If I’ve got two types that already have a particularmethod signature, but the types have noth<strong>in</strong>g else <strong>in</strong> common or don’t know aboutmy code at all, why can’t I “pretend” they implement an <strong>in</strong>terface conta<strong>in</strong><strong>in</strong>g that signature?This feature is known as duck typ<strong>in</strong>g (and yet aga<strong>in</strong>, different people use theterm to mean slightly different th<strong>in</strong>gs) <strong>in</strong> that if someth<strong>in</strong>g walks like a duck andquacks like a duck, you might as well treat it like a duck.If I’m work<strong>in</strong>g with an API (usually through COM) that doesn’t have strongly typedmethods, why can’t I ask the compiler to just stop us<strong>in</strong>g static typ<strong>in</strong>g for a while? VisualBasic has this feature, which is controlled with Option Strict; likewise, implicit typ<strong>in</strong>gis turned on and off us<strong>in</strong>g Option Explicit. Both of these features have received badpress, partly due to poor choices for default values <strong>in</strong> the past. It’s also partly due toLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!