Skip Navigation Links.
C# - Class Access Modifiers
Language(s):C#
Category(s):Class Access Modifiers
Class access modifiers can be a bit confusing. Here's a simple chart and test project you can use to keep track of who can call whom.

C# - Class Access Modifiers

C#

Class Access Modifiers

Class access modifiers can be a bit confusing. Here's a simple chart and test project you can use to keep track of who can call whom.

Having missed an interview question about Access Modifiers using C# - I figured I'd better nail the issue of Access Modifiers in C# into my head once and for all. 

Of course by Access Modifiers I am referring to the scope allowed for the class, property, method or event: public, private, protected, internal and protected internal. Here is a chart that lists the different types of access modifiers and how they work:

public:

            Callable from its own class:               Yes

            Callable from its own assembly:        Yes

            Callable from a different assembly:   Yes

            Callable from a derived class:            Yes  

 

private

            Callable from its own class:                Yes

            Callable from its own assembly:          No

            Callable from a different assembly:     No

            Callable from a derived class:              No  

 

protected

            Callable from its own class:                Yes

            Callable from its own assembly:          No

            Callable from a different assembly:     No

            Callable from a derived class:             Yes  

 

internal

            Callable from its own class:                Yes

            Callable from its own assembly:         Yes

            Callable from a different assembly:     No

            Callable from a derived class:              No  

protected internal

            Callable from its own class:                Yes

            Callable from its own assembly:         Yes

            Callable from a different assembly:     No

            Callable from a derived class:             Yes  

 

So to illustrate the same idea in code:

namespace ExternalClassLibrary

{

    public class clsBaseClass

    {

            .

            .

            .

    }

 

    /// <summary>

    /// clsDerivedClass - Note that this class is declared in 

    ///                   the same namespace as the base class

    /// </summary>

    class clsDerivedClass

    {

        //This class can call anything in the

        //base class declared as

        //public, protected, internal or protected internal

            .

            .

            .

    }

 }

But - if you have a derived class in another *assembly* you cannot call the internal member:

namespace AnotherNamespace

{

    /// <summary>

    /// clsAnotherDerivedClass - Note that this class is declared in 

    ///                   the same namespace as the base class

    /// </summary>

    class clsAnotherDerivedClass

    {

        //This class can call anything in the

        //base class declared as

        //public, protected or protected internal

        //but not internal

            .

            .

            .

    }

 }

 

So - a derived class *can* call a method declared as internal in the base class *if* the derived class is in the same assembly but it *cannot* call the internal method if the derived class is in a *different* assembly.

You can always call a base class method declared as protected or protected internal from the derived class.

A non-derived class can call a method in another class declared as internal if the non-derived class is in the same assembly - otherwise no. A non-derived class can *not* call a protected class in another class.

From: Idioma - 2009-03-30
Test

This article has been viewed 8522 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.