Encapsulation

Even though the previous discussion has pretty much introduced this concept already, let’s bring it over to the world of C# first. Encapsulation enables members (including fields for state, as well as all the other members discussed in Chapters 11, 12, and 13) to be associated with a type. In fact, C# doesn’t allow global members that are defined outside a type (even though the CLR does). In this world, our counter looks like this:

image

We already know from Chapter 10, “Methods,” what keywords such as static and public mean. All we’re emphasizing here is the fact that you put members in types, which will be restricted to classes (and ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.