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.

76 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsI deliberately made RefSample a struct (and therefore a value type) to emphasize thedifference between the constra<strong>in</strong>ed type parameter and the type itself. RefSample is still a value type with value semantics everywhere—it just happens to usethe str<strong>in</strong>g type wherever T is specified <strong>in</strong> its API.When a type parameter is constra<strong>in</strong>ed this way, you can compare references (<strong>in</strong>clud<strong>in</strong>gnull) with == and !=, but be aware that unless there are any other constra<strong>in</strong>ts, onlyreferences will be compared, even if the type <strong>in</strong> question overloads those operators (asstr<strong>in</strong>g does, for example). With a derivation type constra<strong>in</strong>t (described <strong>in</strong> a littlewhile), you can end up with “compiler guaranteed” overloads of == and !=, <strong>in</strong> whichcase those overloads are used—but that’s a relatively rare situation.VALUE TYPE CONSTRAINTSThis constra<strong>in</strong>t (expressed as T : struct) ensures that the type argument used is avalue type, <strong>in</strong>clud<strong>in</strong>g enums. It excludes nullable types (as described <strong>in</strong> chapter 4),however. Let’s look at an example declaration:class ValSample where T : structValid closed types <strong>in</strong>clude■■ValSampleValSampleInvalid closed types <strong>in</strong>clude■■ValSampleValSampleThis time ValSample is a reference type, despite T be<strong>in</strong>g constra<strong>in</strong>ed to be a valuetype. Note that System.Enum and System.ValueType are both reference types <strong>in</strong>themselves, so aren’t allowed as valid type arguments for ValSample. Like referencetype constra<strong>in</strong>ts, when there are multiple constra<strong>in</strong>ts for a particular type parameter,a value type constra<strong>in</strong>t must be the first one specified. When a type parameter is constra<strong>in</strong>edto be a value type, comparisons us<strong>in</strong>g == and != are prohibited.I rarely f<strong>in</strong>d myself us<strong>in</strong>g value or reference type constra<strong>in</strong>ts, although we’ll see<strong>in</strong> the next chapter that nullable types rely on value type constra<strong>in</strong>ts. The rema<strong>in</strong><strong>in</strong>gtwo constra<strong>in</strong>ts are likely to prove more useful to you when writ<strong>in</strong>g your owngeneric types.CONSTRUCTOR TYPE CONSTRAINTSThe third k<strong>in</strong>d of constra<strong>in</strong>t (which is expressed as T : new() and must be the lastconstra<strong>in</strong>t for any particular type parameter) simply checks that the type argumentused has a parameterless constructor, which can be used to create an <strong>in</strong>stance. Thisapplies to any value type; any nonstatic, nonabstract class without any explicitlydeclared constructors; and any nonabstract class with an explicit public parameterlessconstructor.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!