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.

Namespace aliases193same syntax is also available for <strong>in</strong>dexers as well as properties. You could make the settermore public than the getter (a protected getter and a public setter, for example)but that’s a pretty rare situation, <strong>in</strong> the same way that write-only properties are few andfar between compared with read-only properties.NOTETrivia: The only place where “private” is required—Everywhere else <strong>in</strong> <strong>C#</strong>, thedefault access modifier <strong>in</strong> any given situation is the most private one possible.In other words, if someth<strong>in</strong>g can be declared to be private, then leav<strong>in</strong>gout the access modifiers entirely will default it to be<strong>in</strong>g private. This isa nice element of language design, because it’s hard to get it wrong accidentally:if you want someth<strong>in</strong>g to be more public than it is, you’ll noticewhen you try to use it. If, however, you accidentally make someth<strong>in</strong>g “toopublic,” then the compiler can’t help you to spot the problem. For this reason,my personal convention is not to use any access modifiers unless Ineed to; that way, the code highlights (by way of an access modifier) whenI’ve explicitly chosen to make someth<strong>in</strong>g more public than it might be.Specify<strong>in</strong>g the access of a property getter or setter is the one exception tothis rule—if you don’t specify anyth<strong>in</strong>g, the default is to give the getter/setter the same access as the overall property itself.Note that you can’t declare the property itself to be private and make the getter public—youcan only make a particular getter/setter more private than the property. Also,you can’t specify an access modifier for both the getter and the setter—that would justbe silly, as you could declare the property itself to be whichever is the more public ofthe two modifiers.This aid to encapsulation is long overdue. There’s still noth<strong>in</strong>g <strong>in</strong> <strong>C#</strong> 2 to stopother code <strong>in</strong> the same class from bypass<strong>in</strong>g the property and go<strong>in</strong>g directly to whateverfields are back<strong>in</strong>g it, unfortunately. As we’ll see <strong>in</strong> the next chapter, <strong>C#</strong> 3 fixes this<strong>in</strong> one particular case, but not <strong>in</strong> general. Maybe <strong>in</strong> <strong>C#</strong> 4…We move from a feature you may well want to use fairly regularly to one that youwant to avoid most of the time—it allows your code to be absolutely explicit <strong>in</strong> termsof which types it’s referr<strong>in</strong>g to, but at a significant cost to readability.7.4 Namespace aliasesNamespaces are simply ways of keep<strong>in</strong>g fully qualified names of types dist<strong>in</strong>ct even whenthe unqualified names may be the same. An example of this is the unqualified nameButton. There are two classes with that name <strong>in</strong> the .NET 2.0 Framework: System.W<strong>in</strong>dows.Forms.Button and System.Web.UI.WebControls.Button. Although they’reboth called Button, it’s easy to tell them apart by their namespaces. This mirrors real lifequite closely—you may know several people called Jon, but you’re unlikely to know anyoneelse called Jon Skeet. If you’re talk<strong>in</strong>g with friends <strong>in</strong> a particular context, you maywell be able to just use the name Jon without specify<strong>in</strong>g exactly which one you’re talk<strong>in</strong>gabout—but <strong>in</strong> other contexts you may need to provide more exact <strong>in</strong>formation.The us<strong>in</strong>g directive of <strong>C#</strong> 1 (not to be confused with the us<strong>in</strong>g statement that callsDispose automatically) was available <strong>in</strong> two flavors—one created an alias for aLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!