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.

Extension methodsThis chapter covers■■■Writ<strong>in</strong>g extension methodsCall<strong>in</strong>g extension methodsMethod cha<strong>in</strong><strong>in</strong>g■ Extension methods <strong>in</strong> .NET 3.5■Other uses for extension methodsI’m not a fan of <strong>in</strong>heritance. Or rather, I’m not a fan of a number of places where<strong>in</strong>heritance has been used <strong>in</strong> code that I’ve ma<strong>in</strong>ta<strong>in</strong>ed, or class libraries I’veworked with. As with so many th<strong>in</strong>gs, it’s powerful when used properly, but it’s got adesign overhead to it that is often overlooked and can become pa<strong>in</strong>ful over time.It’s sometimes used as a way of add<strong>in</strong>g extra behavior and functionality to a class,even when no real <strong>in</strong>formation is be<strong>in</strong>g added about the object—where noth<strong>in</strong>g isbe<strong>in</strong>g specialized.Sometimes that’s appropriate—if objects of the new type should carry aroundthe details of the extra behavior—but often it’s not. Indeed, often it’s just not possibleto use <strong>in</strong>heritance <strong>in</strong> this way <strong>in</strong> the first place—if you’re work<strong>in</strong>g with a valuetype, a sealed class, or an <strong>in</strong>terface. The alternative is usually to write a bunch ofstatic methods, most of which take an <strong>in</strong>stance of the type <strong>in</strong> question as at least255Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!