Prev | Current Page 213 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

In some
cases (particularly if the interface did require you to return an IList
), this would be a good thing to do anyway??”it keeps the
list returned separate from the internal list. You could even use List.Convert-
All to do it in a single line. It involves copying everything in the list, though,
which may be an unnecessary expense if you trust your callers to use the
returned list reference appropriately.
?–  Make the interface generic, with the type parameter representing the actual type
of storage sublocation being represented. For instance, FabulousStorage-
Location might implement IStorageLocation.
It looks a little odd, but this recursive-looking use of generics can be quite useful
at times.12
?–  Create a generic helper method (preferably in a common class library) that
converts IEnumerator to IEnumerator, where TSource
derives from TDest.
11 Yes, another one.
12 For instance, you might have a type parameter T with a constraint that any instance can be compared to another
instance of T for equality??”in other words, something like MyClass where T : IEquatable.
105 Limitations of generics in C# and other languages
When you run into covariance issues, you may need to consider all of these options
and anything else you can think of. It depends heavily on the exact nature of the situation.


Pages:
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225