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 of the following operators may be used to assign one object to another?The = operator may be used to assign one object's data to another object, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object.
When a member is declared it can be accessed before any objects of its class are created?Explanation: When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static.
Which type of member function may only be called from a function that is a member of the same class?A private member function may only be called from a function that is a member of the same class.
Which type of function is not a member of a class but has access to the private members of the class?A friend function is a function that isn't a member of a class but has access to the class's private and protected members.
|