.NET access modifiers

Discussion in 'C#' started by shabbir, Dec 6, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    Introduction



    Classes and structs can be restricted so that only the program or namespace they are declared in may use them. Class members can be restricted so that only derived classes can use them, or restricted so that only classes within the current namespace or program can use them. Access modifiers are keywords added to the class, struct, or member declaration to specify these restrictions. So in a nutshell access modifiers are keywords which control the visibility of class members and other code constructs.

    The access modifiers in .NET are
    1. public

    2. private

    3. protected

    4. internal

    5. protected internal

    public



    Public means visible to everyone and everywhere.

    Access cases
    1. By objects of the class

    2. By derived classes​

    private



    Private means hidden and usable only by the class itself. No code using a class instance can access a private member and neither can a derived class. Information or functionality that will not be needed or has no meaning outside of the context of a specific class should be made private.

    Access cases
    1. Cannot be accessed by object

    2. Cannot be accessed by derived classes​

    protected



    Protected members are similar to private ones in that they are accessible only by the containing class. However, protected members also may be used by a descendant class. So members that are likely to be needed by a descendant class should be marked protected.

    Access cases
    1. Cannot be accessed by object

    2. By derived classes​

    internal



    Internal are public to the entire assembly but private to any outside assemblies. Internal is useful when you don't want to allow other assemblies to have the functionality.

    Access cases

    In same assembly (public).
    1. By objects of the class

    2. By derived classes ​
    In other assembly (internal)
    1. Cannot be accessed by object

    2. Cannot be accessed by derived classes​

    protected internal



    Finally, we have the only compound access modifier allowed in .NET. Members marked as protected internal may be accessed only by a descendant class that's contained in the same assembly as its base class. You use protected internal in situations where you want to deny access to parts of a class' functionality to any descendant classes found in other applications.

    Note: that it's illegal to combine two access modifiers for a class but can only be applied to the members.

    Access cases

    In same assembly (protected).
    1. Cannot be accessed by object

    2. Can be accessed by a derived classes​
    In other assembly (internal)
    1. Cannot be accessed by object

    2. Cannot be accessed by derived classes​

    Point to remember



    Interface and enumeration members are always public and no access modifiers are needed (or allowed).

    Classes in namespaces are internal by default and may be either internal or public but cannot be private or protected, while namespaces themselves are always public.

    Members of a struct are private by default and may be given public, internal, or private access modifiers.

    To summarize it
    Code:
    +------------------+---------+-----------+--------+----------+--------------------+
    |                  | private | protected | public | internal | protected internal |
    +------------------+---------+-----------+--------+----------+--------------------+
    | By object        | No      | No        | Yes    | Yes      | No                 |
    +------------------+---------+-----------+--------+----------+--------------------+
    | By derived class | No      | Yes       | Yes    | Yes      | Yes(Same assembly) |
    +------------------+---------+-----------+--------+----------+--------------------+
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice