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.

190 CHAPTER 7 Conclud<strong>in</strong>g <strong>C#</strong> 2: the f<strong>in</strong>al featurescheaply later on, and may not even be required, you should leave the statement <strong>in</strong> thefirst form and avoid an unnecessary load <strong>in</strong> some cases.To be honest, unless you’re writ<strong>in</strong>g your own code generators, you’re more likely tobe implement<strong>in</strong>g partial methods than declar<strong>in</strong>g and call<strong>in</strong>g them. If you’re only implement<strong>in</strong>gthem, you don’t need to worry about the argument evaluation side of th<strong>in</strong>gs.In summary, partial methods <strong>in</strong> <strong>C#</strong> 3 allow generated code to <strong>in</strong>teract with handwrittencode <strong>in</strong> a rich manner without any performance penalties for situations where the<strong>in</strong>teraction is unnecessary. This is a natural cont<strong>in</strong>uation of the <strong>C#</strong> 2 partial types feature,which enables a much more productive relationship between code-generationtools and developers.Our next feature is entirely different, and is just a way of tell<strong>in</strong>g the compiler moreabout the <strong>in</strong>tended nature of a type so that it can perform more check<strong>in</strong>g on both thetype itself and any code us<strong>in</strong>g it.7.2 Static classesOur second new feature is <strong>in</strong> some ways completely unnecessary—it just makes th<strong>in</strong>gstidier and a bit more elegant when you write utility classes.Everyone has utility classes. I haven’t seen a significant project <strong>in</strong> either Java or <strong>C#</strong>that didn’t have at least one class consist<strong>in</strong>g solely of static methods. The classic exampleappear<strong>in</strong>g <strong>in</strong> developer code is a type with str<strong>in</strong>g helper methods, do<strong>in</strong>g anyth<strong>in</strong>gfrom escap<strong>in</strong>g, revers<strong>in</strong>g, smart replac<strong>in</strong>g—you name it. An example from the Frameworkis the System.Math class. The key features of a utility class are as follows:■ All members are static (except a private constructor).■ The class derives directly from object.■ Typically there’s no state at all, unless there’s some cach<strong>in</strong>g or a s<strong>in</strong>gleton<strong>in</strong>volved.■ There are no visible constructors.■ The class is sealed if the developer remembers to do so.The last two po<strong>in</strong>ts are optional, and <strong>in</strong>deed if there are no visible constructors(<strong>in</strong>clud<strong>in</strong>g protected ones) then the class is effectively sealed anyway. Both of them helpmake the purpose of the class more obvious, however.List<strong>in</strong>g 7.3 gives an example of a <strong>C#</strong> 1 utility class—then we’ll see how <strong>C#</strong> 2improves matters.List<strong>in</strong>g 7.3A typical <strong>C#</strong> 1 utility classus<strong>in</strong>g System;public sealed class Str<strong>in</strong>gHelper{private Str<strong>in</strong>gHelper(){}BSeals class toprevent derivationPrevents <strong>in</strong>stantiationfrom other codeLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!