13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

192 CHAPTER 7 Conclud<strong>in</strong>g <strong>C#</strong> 2: the f<strong>in</strong>al featuresthe class, it prevents any use of it that would require one. For <strong>in</strong>stance, all of the follow<strong>in</strong>gare <strong>in</strong>valid when Str<strong>in</strong>gHelper is a static class:Str<strong>in</strong>gHelper variable = null;Str<strong>in</strong>gHelper[] array = null;public void Method1(Str<strong>in</strong>gHelper x) {}public Str<strong>in</strong>gHelper Method1() { return null; }List x = new List();None of these are prevented if the class just follows the <strong>C#</strong> 1 pattern—but all of themare essentially useless. In short, static classes <strong>in</strong> <strong>C#</strong> 2 don’t allow you to do anyth<strong>in</strong>g youcouldn’t do before—but they prevent you from do<strong>in</strong>g th<strong>in</strong>gs that you shouldn’t havebeen do<strong>in</strong>g anyway.The next feature on our list is one with a more positive feel. It’s aimed at a very specific—althoughwidely encountered—situation, and allows a solution that is neitherugly nor breaks encapsulation, which was the choice available <strong>in</strong> <strong>C#</strong> 1.7.3 Separate getter/setter property accessI’ll admit to be<strong>in</strong>g slightly bemused when I first saw that <strong>C#</strong> 1 didn’t allow you to havea public getter and a private setter for properties. This isn’t the only comb<strong>in</strong>ation ofaccess modifiers that is prohibited by <strong>C#</strong> 1, but it’s the most commonly desired one. Infact, <strong>in</strong> <strong>C#</strong> 1 both the getter and the setter have to have the same accessibility—it’sdeclared as part of the property declaration rather than as part of the getter or setter.There are perfectly good reasons to want different accessibility for the getter andthe setter—often you may want some validation, logg<strong>in</strong>g, lock<strong>in</strong>g, or other code to beexecuted when chang<strong>in</strong>g a variable that backs the property but you don’t want tomake the property writable to code outside the class. In <strong>C#</strong> 1 the alternatives wereeither to break encapsulation by mak<strong>in</strong>g the property writable aga<strong>in</strong>st your betterjudgment or to write a SetXXX() method <strong>in</strong> the class to do the sett<strong>in</strong>g, which franklylooks ugly when you’re used to “real” properties.<strong>C#</strong> 2 fixes the problem by allow<strong>in</strong>g either the getter or the setter to explicitly have amore restrictive access than that declared for the property itself. This is most easilyseen with an example:str<strong>in</strong>g name;public str<strong>in</strong>g Name{get { return name; }private set{// Validation, logg<strong>in</strong>g etc herename = value;}}In this case, the Name property is effectively read-only to all other types, 2 but we canuse the familiar property syntax for sett<strong>in</strong>g the property with<strong>in</strong> the type itself. The2Except nested types, which always have access to private members of their enclos<strong>in</strong>g types.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!