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.

46 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1a str<strong>in</strong>g array, and will reject attempts to store references to nonstr<strong>in</strong>gs. Covariance isoften useful, but comes at the cost of some of the type safety be<strong>in</strong>g implemented atexecution time <strong>in</strong>stead of compile time.Let’s compare this with the situation that the weakly typed collections such as Array-List and Hashtable put us <strong>in</strong>. The API of these collections uses object as the type ofkeys and values. When you are writ<strong>in</strong>g a method that takes an ArrayList, for example,there is no way of mak<strong>in</strong>g sure at compile time that the caller will pass <strong>in</strong> a list of str<strong>in</strong>gs.You can document it, and the type safety of the runtime will enforce it if you cast eachelement of the list to str<strong>in</strong>g, but you don’t get compile-time type safety. Likewise, if youreturn an ArrayList, you can <strong>in</strong>dicate <strong>in</strong> the documentation that it will just conta<strong>in</strong>str<strong>in</strong>gs, but callers will have to trust that you’re tell<strong>in</strong>g the truth, and will have to <strong>in</strong>sertcasts when they access the elements of the list.F<strong>in</strong>ally, consider the strongly typed collections such as Str<strong>in</strong>gCollection. Theseprovide an API that is strongly typed, so you can be confident that when you receivea Str<strong>in</strong>gCollection as a parameter or return value it will only conta<strong>in</strong> str<strong>in</strong>gs, andyou don’t need to cast when fetch<strong>in</strong>g elements of the collection. It sounds ideal, butthere are two problems. First, it implements IList, so you can still try to add nonstr<strong>in</strong>gsto it (although you’ll fail at runtime). Second, it only deals with str<strong>in</strong>gs.There are other specialized collections, but all told they don’t cover much ground.There’s the CollectionBase type, which can be used to build your own stronglytyped collections, but that means creat<strong>in</strong>g a new collection type for each elementtype, which is also not ideal.Now that we’ve seen the problem with collections, let’s consider the issue that canoccur when overrid<strong>in</strong>g methods and implement<strong>in</strong>g <strong>in</strong>terfaces. It’s related to the ideaof covariance, which we’ve already seen with arrays.LACK OF COVARIANT RETURN TYPESICloneable is one of the simplest <strong>in</strong>terfaces <strong>in</strong> the framework. It has a s<strong>in</strong>gle method,Clone, which should return a copy of the object that the method is called on. Now,leav<strong>in</strong>g aside the issue of whether this should be a deep or shallow copy, let’s look atthe signature of the Clone method:object Clone()It’s a straightforward signature, certa<strong>in</strong>ly—but as I said, the method should return acopy of the object it’s called on. That means it needs to return an object of the sametype—or at least a compatible one (where that mean<strong>in</strong>g will vary depend<strong>in</strong>g on thetype). It would make sense to be able to override the method with a signature that givesa more accurate description of what the method actually returns. For example, <strong>in</strong> aPerson class it would be nice to be able to implement ICloneable withpublic Person Clone()That wouldn’t break anyth<strong>in</strong>g—code expect<strong>in</strong>g any old object would still work f<strong>in</strong>e. Thisfeature is called return type covariance but unfortunately, <strong>in</strong>terface implementation andmethod overrid<strong>in</strong>g don’t support it. Instead, the normal workaround for <strong>in</strong>terfaces isto use explicit <strong>in</strong>terface implementation to achieve the desired effect:Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!