#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");

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

7 Responses to #322 – Class Accessibility

  1. Satyabrata(India) says:

    The Best explanation of access modifiers..great Job!! Sean

  2. Pingback: #614 – Subclass Accessibility « 2,000 Things You Should Know About C#

  3. Pingback: #620 – Inherit from a Class In A Different Assembly « 2,000 Things You Should Know About C#

  4. Pingback: #699 – Types Are Implicitly Internal « 2,000 Things You Should Know About C#

  5. Christopher Wodarczyk says:

    Just started reading these and they are great… however, classes can also be protected/private, if they are defined as inner classes to another class (you might have mentioned that in another post, I’m not sure..)

Leave a comment