24 out of 75 rated this helpful - Rate this topic

Access Modifiers (C# Programming Guide)

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. Those keywords are public, private, protected, and internal. For example:

public class Bicycle
{
    public void Pedal() { }
}

Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.

Class or struct members can be declared with one of five types of access. They can be public or internal, like the classes and structs themselves. A class member can be declared as protected using the protected keyword, meaning that only derived types using the class as a base can access the member. By combining the protected and internal keywords, a class member can be marked protected internal — only derived types or types within the same assembly can access that member. Finally, a class or struct member can be declared as private with the private keyword, indicating that only the class or struct declaring the member is allowed access to that member.

To set the access level for a class or struct member, add the appropriate keyword to the member declaration. Some examples:

// public class:
public class Tricycle
{
    // protected method:
    protected void Pedal() { }

    // private field:
    private int m_wheels = 3;

    // protected internal property:
    protected internal int Wheels
    {
        get { return m_wheels; }
    }
}

Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied.

Namespaces and enumeration members are always public, and no access modifiers can be applied.

Delegates have internal access by default.

Any types declared within a namespace or at the top level of a compilation unit (for example, not within a namespace, class, or struct) are internal by default, but can be made public.

For more information, see the following sections in the C# Language Specification:

  • 10.2.3 Access Modifiers

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Class and Interface Accessibility Question
On this page of the documentation (in the section titled "Class and Struct Accessibility"), it says: $0Classes and structs are declared as internal by default unless the keyword public is added to the class definition...$0 $0$0 $0 $0But in the section titled "Other Types," it says: $0 $0Unlike classes, interfaces default to internal access.$0 $0$0 $0 $0The first says that classes are internal by default, but the second says that interfaces are unlike classes in that interfaces are internal by default. Unless I'm reading it wrong, this has got to be a mistake. $0 $0 CSTeam edit: This has been clarified in later versions of the documentation. Thanks.$0

-------------
Ryan Liu:
Interesting, I noticed same problem and puzzled too. CS Team, why don't you fix this version as well? Very often search engine lead user to this version.

Another related comment, there is a link to C# Language Specification page, where we can download Spec. It is now in html format (great, but it still claims it is Word format). And why don't MS just host this HTML spec somewhere, instead making user to download and view it?

Those tiny things cost developers a few minutes, and there are millions and millions developers access those items.

Thanks!

Giving other assemblies access to internal members
In the AssemblyInfo.cs file of the assembly containing the internal items, you can grant visibility to other classes in other assemblies' namespaces by using the following syntax:

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("YourNamespace.GoesHere")]
Advertisement