13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Type system characteristics45for a different type. You can try by add<strong>in</strong>g a cast to give the compiler this extra (and<strong>in</strong>correct) <strong>in</strong>formation, but if the compiler spots that it’s actually impossible for thatcast to work, it will trigger a compilation error—and if it’s theoretically allowed butactually <strong>in</strong>correct at execution time, the CLR will throw an exception.Now that we know a little about how <strong>C#</strong> 1 fits <strong>in</strong>to the bigger picture of type systems,I’d like to mention a few downsides of its choices. That’s not to say the choicesare wrong—just limit<strong>in</strong>g <strong>in</strong> some ways. Often language designers have to choosebetween different paths that add different limitations or have other undesirable consequences.I’ll start with the case where you want to tell the compiler more <strong>in</strong>formation,but there’s no way of do<strong>in</strong>g so.2.2.2 When is <strong>C#</strong> 1’s type system not rich enough?There are two common situations where you might want to expose more <strong>in</strong>formationto the caller of a method, or perhaps force the caller to limit what they provide <strong>in</strong>their arguments. The first <strong>in</strong>volves collections, and the second <strong>in</strong>volves <strong>in</strong>heritanceand overrid<strong>in</strong>g methods or implement<strong>in</strong>g <strong>in</strong>terfaces. We’ll exam<strong>in</strong>e each <strong>in</strong> turn.COLLECTIONS, STRONG AND WEAKHav<strong>in</strong>g avoided the terms strong and weak for the <strong>C#</strong> type system <strong>in</strong> general, I’ll usethem when talk<strong>in</strong>g about collections. They’re used almost everywhere <strong>in</strong> this context,with little room for ambiguity. Broadly speak<strong>in</strong>g, three k<strong>in</strong>ds of collection types arebuilt <strong>in</strong>to .NET 1.1:■■■Arrays—strongly typed—which are built <strong>in</strong>to both the language and the runtimeThe weakly typed collections <strong>in</strong> the System.Collections namespaceThe strongly typed collections <strong>in</strong> the System.Collections.SpecializednamespaceArrays are strongly typed, 8 so at compile time you can’t set an element of a str<strong>in</strong>g[] tobe a FileStream, for <strong>in</strong>stance. However, reference type arrays also support covariance,which provides an implicit conversion from one type of array to another, so long asthere’s a conversion between the element types. Checks occur at execution time tomake sure that the wrong type of reference isn’t actually stored, as shown <strong>in</strong> list<strong>in</strong>g 2.3.List<strong>in</strong>g 2.3Demonstration of the covariance of arrays, and execution time type check<strong>in</strong>gstr<strong>in</strong>g[] str<strong>in</strong>gs = new str<strong>in</strong>g[5];object[] objects = str<strong>in</strong>gs;objects[0] = new object();BCApplies covariantconversionAttempts to storea pla<strong>in</strong> “object”If you run list<strong>in</strong>g 2.3, you will see an ArrayTypeMismatchException is thrown C. Thisis because the conversion from str<strong>in</strong>g[] to object[] B returns the orig<strong>in</strong>al reference—bothstr<strong>in</strong>gs and objects refer to the same array. The array itself “knows” it is8At least, the language allows them to be. You can use the Array type for weakly typed access to arrays, though.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!