Events
17 Mar, 21 - 21 Mar, 10
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
class
or struct
can access this member.class
or in a derived class
can access this type or member.The record
modifier on a type causes the compiler to synthesize extra members. The record
modifier doesn't affect the default accessibility for either a record class
or a record struct
.
Caller's location | public |
protected internal |
protected |
internal |
private protected |
private |
file |
---|---|---|---|---|---|---|---|
Within the file | ✔️️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
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 the containing type constrains the accessibility of its members.
Multiple declarations of a partial class or partial member must have the same accessibility. If one declaration of the partial class or member doesn't include an access modifier, the other declarations can't declare an access modifier. The compiler generates an error if multiple declarations for the partial class or method declare different accessibilities.
Classes and structs declared directly within a namespace (aren't nested within other classes or structs) can have public
, internal
or file
access. 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.
Derived classes 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.
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 might include any access modifier. You use access modifiers on interface
members to provide a common implementation needed by all implementors of an interface.
A delegate
type declared directly in a namespace has internal
access by default.
For more information about access modifiers, see the Accessibility Levels page.
Members of a class
or struct
(including nested classes 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.
To set the access level for a class
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; }
}
}
Finalizers can't have accessibility modifiers. Members of an enum
type are always public
, and no access modifiers can be applied.
The file
access modifier is allowed only on top-level (non-nested) type declarations.
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 21 - 21 Mar, 10
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Implement Class Properties and Methods - Training
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.
Documentation
internal keyword - C# reference
internal - C# Reference
Static Classes and Static Class Members - C#
Static classes can't be instantiated in C#. You access the members of a static class by using the class name itself.
sealed modifier - C# reference
sealed modifier - C# Reference