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 basics77NOTE<strong>C#</strong> vs. CLI standards—There is a discrepancy between the <strong>C#</strong> and CLIstandards when it comes to value types and constructors. The CLI specificationstates that value types can’t have parameterless constructors, butthere’s a special <strong>in</strong>struction to create a value without specify<strong>in</strong>g anyparameters. The <strong>C#</strong> specification states that all value types have a defaultparameterless constructor, and it uses the same syntax to call both explicitlydeclared constructors and the parameterless one, rely<strong>in</strong>g on the compilerto do the right th<strong>in</strong>g underneath. You can see this discrepancy atwork when you use reflection to f<strong>in</strong>d the constructors of a value type—you won’t see a parameterless one.Aga<strong>in</strong>, let’s look at a quick example, this time for a method. Just to show how it’s useful,I’ll give the implementation of the method too.public T CreateInstance() where T : new(){return new T();}This method just returns a new <strong>in</strong>stance of whatever type you specify, provid<strong>in</strong>gthat it has a parameterless constructor. So CreateInstance(); and Create-Instance(); are OK, but CreateInstance(); isn’t, because str<strong>in</strong>gdoesn’t have a parameterless constructor.There is no way of constra<strong>in</strong><strong>in</strong>g type parameters to force other constructor signatures—for<strong>in</strong>stance, you can’t specify that there has to be a constructor tak<strong>in</strong>g a s<strong>in</strong>glestr<strong>in</strong>g parameter. It can be frustrat<strong>in</strong>g, but that’s unfortunately just the way it is.Constructor type constra<strong>in</strong>ts can be useful when you need to use factory-like patterns,where one object will create another one as and when it needs to. Factories oftenneed to produce objects that are compatible with a certa<strong>in</strong> <strong>in</strong>terface, of course—andthat’s where our last type of constra<strong>in</strong>t comes <strong>in</strong>.DERIVATION TYPE CONSTRAINTSThe f<strong>in</strong>al (and most complicated) k<strong>in</strong>d of constra<strong>in</strong>t lets you specify another type thatthe type argument must derive from (<strong>in</strong> the case of a class) or implement (<strong>in</strong> the caseof an <strong>in</strong>terface). 4 For the purposes of constra<strong>in</strong>ts, types are deemed to derive fromthemselves. You can specify that one type argument must derive from another, too—this is called a type parameter constra<strong>in</strong>t and makes it harder to understand the declaration,but can be handy every so often. Table 3.2 shows some examples of generic typedeclarations with derivation type constra<strong>in</strong>ts, along with valid and <strong>in</strong>valid examples ofcorrespond<strong>in</strong>g constructed types.The third constra<strong>in</strong>t of T : IComparable is just one example of us<strong>in</strong>g a generictype as the constra<strong>in</strong>t. Other variations such as T : List (where U is another type4Strictly speak<strong>in</strong>g, an implicit reference conversion is OK too. This allows for a constra<strong>in</strong>t such as where T :IList to be satisfied by Circle[]. Even though Circle[] doesn’t actually implement IList, there is an implicit reference conversion available.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!