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.

Beyond the basics75parameters. You don’t have to use it, but it can make your code a lot easier to readwhen used appropriately. We’ll see <strong>in</strong> part 3 that the <strong>C#</strong> compiler is gradually be<strong>in</strong>gallowed to <strong>in</strong>fer a lot more <strong>in</strong>formation from your code, while still keep<strong>in</strong>g the languagesafe and statically typed.The last part of this section deals with obta<strong>in</strong><strong>in</strong>g the default value of a type parameterand what comparisons are available when you’re writ<strong>in</strong>g generic code. We’ll wrapup with an example demonstrat<strong>in</strong>g most of the features we’ve covered, as well as be<strong>in</strong>ga useful class <strong>in</strong> itself.Although this section delves a bit deeper <strong>in</strong>to generics, there’s noth<strong>in</strong>g really hardabout it. There’s plenty to remember, but all the features serve a purpose, and you’llbe grateful for them when you need them. Let’s get started.3.3.1 Type constra<strong>in</strong>tsSo far, all the type parameters we’ve seen can be applied to any type at all—they areunconstra<strong>in</strong>ed. We can have a List, a Dictionary, anyth<strong>in</strong>g.That’s f<strong>in</strong>e when we’re deal<strong>in</strong>g with collections that don’t have to <strong>in</strong>teract with whatthey store—but not all uses of generics are like that. Often you want to call methodson <strong>in</strong>stances of the type parameter, or create new <strong>in</strong>stances, or make sure you onlyaccept reference types (or only accept value types). In other words, you want to specifyrules to say which type arguments are considered valid for your generic type ormethod. In <strong>C#</strong> 2, you do this with constra<strong>in</strong>ts.Four k<strong>in</strong>ds of constra<strong>in</strong>ts are available, and the general syntax is the same for all ofthem. Constra<strong>in</strong>ts come at the end of the declaration of a generic method or type,and are <strong>in</strong>troduced by the contextual keyword where. They can be comb<strong>in</strong>ed together<strong>in</strong> sensible ways, as we’ll see later. First, however, we’ll explore each k<strong>in</strong>d of constra<strong>in</strong>t<strong>in</strong> turn.REFERENCE TYPE CONSTRAINTSThe first k<strong>in</strong>d of constra<strong>in</strong>t (which is expressed as T : class and must be the first constra<strong>in</strong>tspecified for that type parameter) simply ensures that the type argument usedis a reference type. This can be any class, <strong>in</strong>terface, array, or delegate—or anothertype parameter that is already known to be a reference type. For example, considerthe follow<strong>in</strong>g declaration:struct RefSample where T : classValid closed types <strong>in</strong>clude■■■RefSampleRefSampleRefSampleInvalid closed types <strong>in</strong>clude■■RefSampleRefSampleLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!