#322 – Class Accessibility

The members of a class all have an associated access modifier, which defines their accessibility.  A class itself also has an accessibility level, which dictates which code can make use of the class.

The two types of accessibility for a class are:

  • public – all code can use the class
  • internal – only code in the same .exe or .dll can use the class

In the code below, the Dog class, defined in DogLibrary.dll, is marked as internal.  This means that only code within the same DLL (highlighted blue) can create and use instances of the Dog class.

The Program class in Program.exe has access to the DogKennel class, but not the Dog class.  It can create an instance of a DogKennel, but not an instance of a Dog.

    class Program
    {
        static void Main()
        {
            // Ok
            DogKennel k = new DogKennel();
            k.EverybodyBark();

            // ERROR
            Dog d = new Dog("Kirby");

#321 – Accessibility of Constants

As with other class members, you can apply access modifiers to constants defined in a class to define their accessibility.  Accessibility dictates what other code has access to the constant’s value.

  • public – All code has access
  • private – Only code in the defining class has access
  • protected – Code in the defining class or derived classes has access
  • internal – All code in the defining assembly has access
  • protected internal – Code in the defining assembly or in derived classes has access

        // All code has access
        public const string PluralOfDog = "Dogs";

        // Only this class has access
        private const Breeds DefaultBreed = Breeds.Retriever;

        // Subclass has access
        protected const string DefaultDogName = "Bowser";

        // Code in same assembly has access
        internal const string TargetArchitecture = "x86";

        // Code in same assembly or subclass has access
        protected internal const int DeckSize = 52;

#315 – Accessibility of Static Methods and Properties

Like instance methods and properties, you can define the accessibility of static methods and properties using access modifiers.  As with instance members, access modifiers on static members indicates what code has access to the member.

  • public – All code has access
  • private – Only code in the defining class has access
  • protected – Code in the defining class or derived classes has access
  • internal – All code in the defining assembly has access
  • protected internal – Code in the defining assembly or in derived classes has access

        // All code has access
        public static Dog MakeNewDog()
        {
        }

        // Only this class has access
        private static void InitSomeStaticData()
        {
        }

        // Subclass has access
        protected static void InitOtherStuff()
        {
        }

        // Code in same assembly has access
        internal static void RunSomeCalcs()
        {
        }

        // Code in same assembly or subclass has access
        protected internal static void DoOtherStuff()
        {
        }

#314 – Access Modifiers Are Not Allowed on Static Constructors

Because you can’t call a static constructor directly, you can’t include an access modifier (e.g. public, private) when defining a static constructor.  Static constructors are defined without access modifiers.

        static Dog()
        {
            Motto = "We serve humans.  And lick ourselves.";
        }

The compiler will generate an error if you try to include an access modifier.

#313 – Accessibility of Constructors in a Class

You can apply access modifiers to instance constructors, dictating what code is allowed to create an instance of the class using a signature that matches the constructor.

  • public – All code has access
  • private – Only code in defining class has access
  • protected – Code in defining class or derived classes has access
  • internal – All code in defining assembly has access
  • protected internal – Code in defining assembly or in derived classes has access

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

        // Only code in this class has access
        private Dog()
        {
            Name = "UNKNOWN";
            Age = 0;
        }

        // Code in this class or subclass has access
        protected Dog(string name)
        {
            Name = name;
            Age = 1;
        }

        // Code in same assembly has access
        internal Dog(int age)
        {
            Name = "INTERNAL";
            Age = age;
        }

        // Code in same assembly or derived classes has access
        protected internal Dog(double age)
        {
            Age = (int)age;
        }

#312 – Accessibility of Methods in a Class

You can apply access modifiers to methods defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to call the method.

  • public – Any code can call the method
  • private – Only code in the defining class can call the method
  • protected – Code in the defining class or derived classes can call the method
  • internal – Any code in the defining assembly can call the method
  • protected internal – Code in the defining assembly or in derived classes can call the method

    public class Dog
    {
        public void BarkALot()
        {
            for (int i = 1; i < 30; i++)
                BarkOnce();
        }

        // Only this class can call
        private void BarkOnce()
        {
            Say("Woof");
        }

        // Subclass can call
        protected void BarkYourName()
        {
            Say(Name);
        }

        // Code in same assembly can call
        internal void DumpBarkCount()
        {
            Say(numBarks);
        }

        // Code in same assembly or subclass can call
        protected internal void BarkNameTwice()
        {
            Say(Name);
            Say(Name);
        }
    }

#311 – Accessibility of Properties in a Class

You can apply access modifiers to properties defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to read and the write the value of a property.

  • public – All code can read/write the property
  • private – Only code in the defining class can read/write the property
  • protected – Code in the defining class or derived classes can read/write the property
  • internal – All code in the defining assembly can read/write the property
  • protected internal – Code in the defining assembly or in derived classes can read/write the property

    public class Dog
    {
        // All code can access
        public string Nickname { get; set; }

        // Only code in this calss can access
        private string genericDogSecretName { get; set; }

        // Code in this class or subclass can access
        protected int totalBarkCount { get; set; }

        // Code in same assembly can access
        internal int invokeCount { get; set; }

        // Code in same assembly or derived classes can access
        protected internal int barkInvokeCount { get; set; }
    }

#310 – Accessibility of Fields in a Class

You can apply access modifiers to fields defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to read and the write the value of a field.

  • public – All code can read/write the field
  • private – Only code in the defining class can read/write the field
  • protected – Code in the defining class or derived classes can read/write the field
  • internal – All code in the defining assembly can read/write the field
  • protected internal – Code in the defining assembly or in derived classes can read/write the field

    public class Dog
    {
        // All code can access
        public string Nickname;

        // Only code in this class can access
        private string genericDogSecretName;

        // Code in this class or subclass can access
        protected int totalBarkCount;

        // Code in same assembly can access
        internal int invokeCount;

        // Code in same assembly or derived classes can access
        protected internal int barkInvokeCount;
    }

#305 – Public Class Members

Class members marked with the accessibility keyword public are accessible from any code.

In the picture below, the Dog.DoBark method is marked as public.  The code in any of the blue blocks 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.

Follow

Get every new post delivered to your Inbox.

Join 43 other followers