Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Article

C# Access Modifiers Quick Reference

Rate me:
Please Sign up or sign in to vote.
3.58/5 (27 votes)
21 May 2008CPOL1 min read 99K   890   36   17
A quick description of access modifiers with a diagram showing accessibility limits
IllustrateAccessInCsharp2.png

Introduction

This is a very small article to illustrate C# access modifiers. This article is for beginners but even if you are an expert, this article may be useful as a quick reminder when the brain cells slow down - hey, I will probably need it soon.

Definition

An access modifier is a keyword you can add to your declaration of a class or class member to allow or restrict their accessibility.

The diagram above illustrates how each access modifier affects the accessibility of the class or member.

Using the Code

I kind of doubt you will need any more than the diagram as a reminder, but since I want to make this article more complete for beginners, here is some more content.

Now, a few examples:

C#
internal class Class1
{
    private int Member1;
    protected int Member2;
    internal int Member3;
    protected internal int Member4;

    private class NestedClass
    {
        void Test(Class1 foo)
        {
            foo.Member1 = 0; //OK
            foo.Member2 = 0; //OK
            foo.Member3 = 0; //OK
            foo.Member4 = 0; //OK
        }
    }
}

C#
public class Class2
{
    private void Method1()
    {
    }

    private int _Member1;

    protected int Member1
    {
        get
        {
            return _Member1;
        }
        set
        {
            Method1();
            _Member1 = value;
        }
    }

    internal int Member2;
    public int Member3;
    protected internal int Member4;
}

The above access modifiers prevent direct access as seen by the following access attempts. These attempts produce compilation errors:

From the same assembly as the above classes:

C#
class Test
{
    void Test1()
    {
        Class1 c1 = new Class1(); //OK
        c1.Member1 = 0; //Compilation error here
        c1.Member2 = 0; //Compilation error here
        c1.Member3 = 0; //OK
        c1.Member4 = 0; //OK
    }
}

internal class DerivedClass : Class1
{
    private void Test2()
    {
        Member1 = 0; //Compilation error here
        Member2 = 0; //OK
        Member3 = 0; //OK
        Member4 = 0; //OK
    }

    private class NestedClass
    {
        void Test(DerivedClass foo)
        {
            foo.Member1 = 0; //Compilation error here
            foo.Member2 = 0; //OK
            foo.Member3 = 0; //OK
            foo.Member4 = 0; //OK
        }
        void Test(Class1 foo)
        {
            foo.Member1 = 0; //Compilation error here
            foo.Member2 = 0; //Compilation error here
            foo.Member3 = 0; //OK
            foo.Member4 = 0; //OK
        }
    }
}

From a different assembly from the above classes:

C#
public class Test
{
    void Test1()
    {
        Class1 c1 = new Class1(); //Compilation error here

        Class2 c2 = new Class2(); //OK
        c2.Method1(); //Compilation error here
        c2._Member1 = 0; //Compilation error here
        c2.Member1 = 0; //Compilation error here
        c2.Member2 = 0; //Compilation error here
        c2.Member3 = 0; //OK
        c2.Member4 = 0; //Compilation error here
    }
}

internal class DerivedClass : Class2
{
    private void Test2()
    {
        Member1 = 0; //OK
        Member2 = 0; //Compilation error here
        Member3 = 0; //OK
        Member4 = 0; //OK
    }
    private class NestedClass
    {
        void Test(DerivedClass foo)
        {
            foo.Member1 = 0; //Compilation error here
            foo.Member2 = 0; //Compilation error here
            foo.Member3 = 0; //OK
            foo.Member4 = 0; //OK
        }
        void Test(Class2 foo)
        {
            foo.Member1 = 0; //Compilation error here
            foo.Member2 = 0; //Compilation error here
            foo.Member3 = 0; //OK
            foo.Member4 = 0; //Compilation error here
        }
    }
}

Points of Interest

In the examples above, you restrict direct access to Class2._Member1 but provide a wrapped property called Member1. The requirement is to ensure Method1 is called when _Member1 changes. The access modifiers ensure that we can only change _Member1 via the property.

That concludes the article.

History

  • 9th April, 2008: Initial version
  • 10th April, 2008: Modified to include more detail
  • 14th April, 2008: Modified diagram

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVery good. Pin
syaifulnizamyahya22-Oct-18 1:12
syaifulnizamyahya22-Oct-18 1:12 
SuggestionReally useful Pin
CoderPanda10-Oct-13 23:48
professionalCoderPanda10-Oct-13 23:48 
GeneralMy vote of 5 Pin
Pratik Bhuva26-Aug-13 3:02
professionalPratik Bhuva26-Aug-13 3:02 
QuestionSummary - "protected" relaxed constraint "internal" and vice versa ("protected internal" = protected OR internal ) Pin
devvvy18-Feb-13 1:32
devvvy18-Feb-13 1:32 
GeneralMy vote of 5 Pin
Sathiyanath11-Jun-12 14:13
Sathiyanath11-Jun-12 14:13 
GeneralMy vote of 5 Pin
mani2009it13-Apr-12 2:15
mani2009it13-Apr-12 2:15 
GeneralMy vote of 5 Pin
devvvy24-Feb-11 19:08
devvvy24-Feb-11 19:08 
GeneralDon't think diagram explains this case (access from NestedClass residing in same assembly) ... Pin
devvvy24-Feb-11 15:12
devvvy24-Feb-11 15:12 
GeneralRe: Don't think diagram explains this case (access from NestedClass residing in same assembly) ... Pin
Johan Fourie24-Feb-11 17:05
Johan Fourie24-Feb-11 17:05 
GeneralRe: Don't think diagram explains this case (access from NestedClass residing in same assembly) ... [modified] Pin
devvvy24-Feb-11 19:07
devvvy24-Feb-11 19:07 
GeneralSuggestion: Properties that are both protected and internal Pin
Danovich9-Apr-08 2:40
Danovich9-Apr-08 2:40 
Your chart shows a boundary called "protected internal", but your test code does not have any protected internal properties.

Having a class in your main assembly that does not inherit from ClassDefiningTheAccess will show better how it reacts to internal and protected internal properties compared to ClassThatInherits2 and ClassThatInherits3.
GeneralRe: Suggestion: Properties that are both protected and internal Pin
Johan Fourie10-Apr-08 0:08
Johan Fourie10-Apr-08 0:08 
GeneralRe: Suggestion: Properties that are both protected and internal Pin
Danovich11-Apr-08 4:29
Danovich11-Apr-08 4:29 
GeneralRe: Suggestion: Properties that are both protected and internal Pin
Johan Fourie12-Apr-08 0:22
Johan Fourie12-Apr-08 0:22 
GeneralRe: Suggestion: Properties that are both protected and internal Pin
Danovich14-Apr-08 5:51
Danovich14-Apr-08 5:51 
GeneralYou are missing the whole aspect of nested classes Pin
leppie9-Apr-08 2:29
leppie9-Apr-08 2:29 
GeneralRe: You are missing the whole aspect of nested classes Pin
Johan Fourie10-Apr-08 0:14
Johan Fourie10-Apr-08 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.