Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Access Modifiers (C# Programming Guide)
In this articleAll 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:
Summary table
The following examples demonstrate how to specify access modifiers on a type and member:
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 accessibilityClasses, records, and structs declared directly within a namespace (in other words,
that aren't nested within other classes or structs) can be either Struct members, including nested classes and structs, can be declared Derived classes and derived records can't have greater accessibility than their base types. You can't declare a public class You can enable specific other assemblies to access your internal types by using the Class, record, and struct member accessibilityClass 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 Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it. However, a 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 User-defined operators must always be declared as Finalizers can't have accessibility modifiers. To set the access level for a
Other typesInterfaces declared directly within a namespace can
be Enumeration members are always Delegates
behave like classes and structs. By default, they have C# language specificationFor more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage. See also
FeedbackSubmit and view feedback for Which modifier is used to specify that a method Cannot be used outside a class?For more information about packages, check out the section on packages. Any variable, method, or class declared to use the default access modifier cannot be accessed by any other class outside of the package from which it was declared.
What can be used to extend an existing class so as to create a more specific class?Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
Which access modifier makes variable and methods visible only in the class where they are declared?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.
What class access modifier means that access to the class is not limited?Chapter 07 Key Terms Review. |