O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Encapsulation

Even though the previous discussion has pretty much introduced the concept of encapsulation already, let’s translate the sample to the C# world first. Encapsulation enables members (including fields for state, as well as all the other members discussed in Chapter 11, “Fields, Properties, and Indexers,” Chapter 12, “Constructors and Finalizers,” and Chapter 13, “Operator Overloading and Conversions”) 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:

class Counter {    private readonly int _id;    private int _i;    private static int s_n;    public Counter() {       _id= s_n++; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required