#819 – A Private Constructor May Prevent Inheritance

You can make a constructor private to restrict creation of an instance of the class only to code within the class.

If all constructors in a class are private, this means that a derived class is also prevented from calling a constructor.  Because the derived class must be able to call some constructor on the parent class, this will effectively prevent the creation of any derived class.

Assume that a Dog class defines a single constructor and makes it private.  In the code shown below, the Terrier class defines a constructor, which would implicitly call the default constructor in the base class.  Because that constructor is private, we get a compiler error and can’t create the Terrier class.

    public class Terrier : Dog
    {
        public double FeistyFactor { get; set; }

        public Terrier(string name, int age, double feistyFactor)
        {
            Name = name;
            Age = age;
            FeistyFactor = feistyFactor;
        }
    }

819-001

Advertisement

#715 – Private Members Are Not Visible in a Derived Class

Class members declared with an accessibility level of private are accessible only within the code for the class in which they are declared.  They are not accessible from a derived class.

In the example below, dogData is a private member of the Dog class, so it is not visible within the Terrier class (which inherits from Dog).

    public class Dog
    {
        // Stuff that's clearly public
        public string Name { get; set; }
        public int Age { get; set; }

        private int dogData;

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
            dogData = 12;
        }
    }
    public class Terrier : Dog
    {
        public Terrier(string name, int age) : base(name, age)
        {
        }

        public void Snarl()
        {
            // Compiler error: dogData is inaccessible due to its protection level
            Console.WriteLine(dogData);
        }
    }

#698 – Type Members Are Implicitly Private

You use access modifiers to define the accessibility of members within a class (publicprivate, protected, internal or protected internal).

If you omit the access modifier entirely, the class member defaults to being private.  This is true for all class members, including constants, fields, properties, methods, indexers, events, constructors and nested types.

In the example below, the Description property, NumDogs static property and the ChaseTail method are all effectively private members, because their declaration does not include an access modifier.

    public class Dog
    {
        // Public properties
        public string Name { get; set; }
        public int Age { get; set; }

        // Implicitly private
        string Description { get; set; }

        // Instance constructor
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        // Implicitly private
        static int NumDogs { get; set; }

        // Implicitly private
        void ChaseTail()
        {
            Console.WriteLine("I'm chasing my tail");
        }
    }

#304 – Private Class Members

Class members marked with the accessibility keyword private are accessible only from code within the same class.

In the picture below, the Dog.DoBark method is marked as private.  Only the code in the blue block can call this method.

#303 – Accessibility of Class Members

Members of a class can have different kinds of accessibility.  An accessibility keyword indicates what source code can access the member.  The different types of accessibility are:

Accessibility Keyword Description
public All code can access the member
private Only other code in the class can access the member
protected Code in this class or any class that inherits from this class can access
internal Code in files in the same assembly (.dll or .exe) can access
protected internal Code in the same assembly or in classes that inherit from this class can access

Accessibility keywords can apply to the following kinds of members of a class: fields, properties, methods, constants, indexers, events, constructors and nested types.  They can apply to both instance and static members.

#253 – Implementing a Read-Only Automatic Property

When using the automatic property syntax,C# requires that you define both a get and a set accessor.  But with the default syntax, you end up with a read/write property.

        public int Age { get; set;  }

But what if we want a read-only property, from the client code’s perspective?  Since we’re required to define the set accessor, it looks like client code would always be able to write to the property.

The solution is that we can add a private access modifier to the set accessor.

        public int Age { get; private set; }

With the set accessor private, code external to the class can read the property’s value, but not write to it.  But code within the class can still write to the property.

#241 – Declaring and Using Private Instance Methods

You can include a method in a class that is private.  It will be callable from other methods within the class, but not callable from outside the class.

In the example below, the PrintName method is visible from outside the class, but the PrintAge method can only be called from within the class.

    public class Dog
    {
        public string Name;
        public int Age;

        public void PrintName()
        {
            Console.Write(Name);
            PrintAge();
        }

        private void PrintAge()
        {
            Console.WriteLine(string.Format(" ({0})", Age));
        }
    }

Code that attempts to call the PrintAge method from outside the class will not compile.

            Dog kirby = new Dog();
            kirby.Name = "Kirby";
            kirby.Age = 14;

            kirby.PrintName();
            kirby.PrintAge();    // compiler error

#240 – Private and Public Instance Data in a Class

In a class, you can define public instance data, as fields.  Any instance of the class can read and write the instance data stored in these fields.

For example:

    public class Dog
    {
        public string Name;
        public int Age;
    }

    // Creating a Dog object
    Dog kirby = new Dog();
    kirby.Name = "Kirby";
    kirby.Age = 14;

You might, however, want to declare some instance data that is visible from within the class’ methods, but not visible outside the class.  You can do this by declaring a field as private.

        public string Name;
        public int Age;

        private DateTime lastPrint;

        public void PrintName()
        {
            Console.WriteLine(Name);

            // lastPrint is visible here
            lastPrint = DateTime.Now;
        }

This private field will not be visible from outside the class.

            Dog kirby = new Dog();
            kirby.Name = "Kirby";

            // Compiler error: inaccessible due to its protection level
            DateTime when = kirby.lastPrint;