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.

Delegation as the new <strong>in</strong>heritance355associated with dynamic languages. In particular, the implicitly typed local variables andarrays, extra type <strong>in</strong>ference capabilities for generic methods, extension methods, andbetter <strong>in</strong>itialization structures are all th<strong>in</strong>gs that <strong>in</strong> some ways look like they belong <strong>in</strong>dynamic languages.While <strong>C#</strong> itself is currently statically typed, the Dynamic Language Runtime (DLR)will br<strong>in</strong>g dynamic languages to .NET. Integration between static languages anddynamic ones such as IronRuby and IronPython should therefore be relativelystraightforward—this will allow projects to pick which areas they want to write dynamically,and which are better kept statically typed.Should <strong>C#</strong> become dynamic <strong>in</strong> the future? Given recent blog posts from the <strong>C#</strong>team, it seems likely that <strong>C#</strong> 4 will allow dynamic lookup <strong>in</strong> clearly marked sections ofcode. Call<strong>in</strong>g code dynamically isn’t the same as respond<strong>in</strong>g to calls dynamically, however—andit’s possible that <strong>C#</strong> will rema<strong>in</strong> statically typed at that level. That doesn’tmean there can’t be a language that is like <strong>C#</strong> <strong>in</strong> many ways but dynamic, <strong>in</strong> the sameway that Groovy is like Java <strong>in</strong> many ways but with some extra features and dynamicexecution. It should be noted that Visual Basic already allows for optionally dynamiclookups, just by turn<strong>in</strong>g Option Strict on and off. In the meantime, we should begrateful for the <strong>in</strong>fluence of dynamic languages <strong>in</strong> mak<strong>in</strong>g <strong>C#</strong> 3 a lot more expressive,allow<strong>in</strong>g us to state our <strong>in</strong>tentions without as much fluff surround<strong>in</strong>g the really usefulbits of code.The changes to <strong>C#</strong> don’t just affect how our source code looks <strong>in</strong> pla<strong>in</strong> text terms,however. They should also make us reconsider the structure of our programs, allow<strong>in</strong>gdesigns to make much greater use of delegates without fear of forc<strong>in</strong>g thousands ofone-l<strong>in</strong>e methods on users.13.2 Delegation as the new <strong>in</strong>heritanceThere are many situations where <strong>in</strong>heritance is currently used to alter the behavior ofa component <strong>in</strong> just one or two ways—and they’re often ways that aren’t so much<strong>in</strong>herent <strong>in</strong> the component itself as <strong>in</strong> how it <strong>in</strong>teracts with the world around it.Take a data grid, for example. A grid may use <strong>in</strong>heritance (possibly of a typerelated to a specific row or column) to determ<strong>in</strong>e how data should be formatted. Inmany ways, this is absolutely right—you can build up a flexible design that allows forall k<strong>in</strong>ds of different values to be displayed, possibly <strong>in</strong>clud<strong>in</strong>g images, buttons,embedded tables, and the like. The vast majority of read-only data is likely to consist ofsome pla<strong>in</strong> text, however. Now, we could have a TextDataColumn type with an abstractFormatData method, and derive from that <strong>in</strong> order to format dates, pla<strong>in</strong> str<strong>in</strong>gs,numbers, and all k<strong>in</strong>ds of other data <strong>in</strong> whatever way we want.Alternatively, we could allow the user to specify the formatt<strong>in</strong>g by way of a delegate,which simply converts the appropriate data type to a str<strong>in</strong>g. With <strong>C#</strong> 3’s lambda expressions,this makes it easy to provide a custom display of the data. Of course, you may wellwant to provide easy ways of handl<strong>in</strong>g common cases—but delegates are immutable <strong>in</strong>.NET, so simple “constant” delegates for frequently used types can fill this need neatly.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!