Access Modifiers (C# Programming Guide) Microsoft Docs

User Manual: Pdf

Open the PDF directly: View PDF PDF.
Page Count: 11

Scroll down to view the document on your mobile browser.
1/14/2018 Access Modifiers (C# Programming Guide) | Microsoft Docshttps://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers 1/11Access Modifiers (C# ProgrammingGuide)In this articleC# Copy07/20/2015 • 4 minutes to read • Contributors          allClass and Struct AccessibilityClass and Struct Member AccessibilityOther TypesC# Language SpecificationSee AlsoAll types and type members have an accessibility level, which controls whether they can be usedfrom other code in your assembly or other assemblies. You can use the following accessmodifiers 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 anotherassembly that references it.+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 derivedfrom that class. internal The type or member can be accessed by any code in the same assembly, but not from anotherassembly.protected internal The type or member can be accessed by any code in the assembly in which itis declared, or from within a derived class in another assembly.1private protected The type or member can be accessed only within its declaring assembly, bycode in the same class or in a type that is derived from that class.The following examples demonstrate how to specify access modifiers on a type and member:
1/14/2018 Access Modifiers (C# Programming Guide) | Microsoft Docshttps://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers 2/11public class Bicycle {     public void Pedal() { } } Class and Struct AccessibilityClass and Struct Member AccessibilityNot all access modifiers can be used by all types or members in all contexts, and in some casesthe accessibility of a type member is constrained by the accessibility of its containing type. Thefollowing sections provide more details about accessibility.Classes and structs that are declared directly within a namespace (in other words, that are notnested within other classes or structs) can be either public or internal. Internal is the default if noaccess modifier is specified.Struct members, including nested classes and structs, can be declared as public, internal, orprivate. Class members, including nested classes and structs, can be public, protected internal,protected, internal, private protected or private. The access level for class members and structmembers, including nested classes and structs, is private by default. Private nested types are notaccessible from outside the containing type.Derived classes cannot have greater accessibility than their base types. In other words, youcannot have a public class  B that derives from an internal class  A. If this were allowed, itwould have the effect of making  A public, because all protected or internal members of  A areaccessible from the derived class.You can enable specific other assemblies to access your internal types by using theInternalsVisibleToAttribute. For more information, see Friend Assemblies.Class members (including nested classes and structs) can be declared with any of the six types ofaccess. Struct members cannot be declared as protected because structs do not supportinheritance.Normally, the accessibility of a member is not greater than the accessibility of the type thatcontains it. However, a public member of an internal class might be accessible from outside theassembly if the member implements interface methods or overrides virtual methods that aredefined in a public base class.

Navigation menu