Which access modifier is accessible only to the classes in the same package and its sub classes?

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Access Modifiers (C# Programming Guide)

  • Article
  • 06/18/2022
  • 4 minutes to read

In this article

All types and type members have an accessibility level. The accessibility level controls whether they can be used from other code in your assembly or other assemblies. An assembly is a .dll or .exe created by compiling one or more .cs files in a single compilation. Use the following access modifiers to specify the accessibility of a type or member when you declare it:

  • public: The type or member can be accessed by any other code in the same assembly or another assembly that references it. The accessibility level of public members of a type is controlled by the accessibility level of the type itself.
  • private: The type or member can be accessed only by code in the same class or struct.
  • protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
  • internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation.
  • protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • private protected: The type or member can be accessed by types derived from the class that are declared within its containing assembly.

Summary table

Caller's locationpublicprotected internalprotectedinternalprivate protectedprivate
Within the class ✔️️ ✔️ ✔️ ✔️ ✔️ ✔️
Derived class (same assembly) ✔️ ✔️ ✔️ ✔️ ✔️
Non-derived class (same assembly) ✔️ ✔️ ✔️
Derived class (different assembly) ✔️ ✔️ ✔️
Non-derived class (different assembly) ✔️

The following examples demonstrate how to specify access modifiers on a type and member:

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

Not all access modifiers are valid for all types or members in all contexts. In some cases, the accessibility of a type member is constrained by the accessibility of its containing type.

Class, record, and struct accessibility

Classes, records, and structs declared directly within a namespace (in other words, that aren't nested within other classes or structs) can be either public or internal. internal is the default if no access modifier is specified.

Struct members, including nested classes and structs, can be declared public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected, or private. Class and struct members, including nested classes and structs, have private access by default. Private nested types aren't accessible from outside the containing type.

Derived classes and derived records can't have greater accessibility than their base types. You can't declare a public class B that derives from an internal class A. If allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies.

Class, record, and struct member accessibility

Class and record members (including nested classes, records and structs) can be declared with any of the six types of access. Struct members can't be declared as protected, protected internal, or private protected because structs don't support inheritance.

Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

The type of any member field, property, or event must be at least as accessible as the member itself. Similarly, the return type and the parameter types of any method, indexer, or delegate must be at least as accessible as the member itself. For example, you can't have a public method M that returns a class C unless C is also public. Likewise, you can't have a protected property of type A if A is declared as private.

User-defined operators must always be declared as public and static. For more information, see Operator overloading.

Finalizers can't have accessibility modifiers.

To set the access level for a class, record, or struct member, add the appropriate keyword to the member declaration, as shown in the following example.

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

    // private field:
    private int _wheels = 3;

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

Other types

Interfaces declared directly within a namespace can be public or internal and, just like classes and structs, interfaces default to internal access. Interface members are public by default because the purpose of an interface is to enable other types to access a class or struct. Interface member declarations may include any access modifier. This is most useful for static methods to provide common implementations needed by all implementors of a class.

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

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also

  • Specify modifier order (style rule IDE0036)
  • C# Programming Guide
  • The C# type system
  • Interfaces
  • private
  • public
  • internal
  • protected
  • protected internal
  • private protected
  • class
  • struct
  • interface

Feedback

Submit and view feedback for

Which access modifier is accessible within the same class?

Private: The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of the same package will not be able to access these members.

Which access modifier is accessing in any class or package?

Public Access Modifier - Public A class, method, constructor, interface, etc. declared public can be accessed from any other class.

Which access modifier is accessible within package and outside the package but through inheritance only?

Protected: We can access the protected modifier within the same package and also from outside the package with the help of the child class. If we do not make the child class, we cannot access it from outside the package. So inheritance is a must for accessing it from outside the package.

Which method is accessible only in the classes in which it is defined?

Private Access Modifier The methods and fields that are declared as private are not accessible outside the class. They are accessible only within the class which has these private entities as its members.