C # access Modifiers

Source: Internet
Author: User
Tags modifiers

All types and type members in C # have accessibility levels to control whether they can be used in other code in your assembly or in other assemblies. You can use the access modifier to specify the accessibility of a type or member when declaring a type or member.

    • Public

Any other code in the same assembly, or other assemblies that reference the assembly, can access the type or member.

    • Private

Only code in the same class or struct can access the type or member.

    • Protected

Only types or members can be accessed by code in the same class or struct, or in a derived class of this class.

    • Internal

Any code in the same assembly can access the type or member, but the code in the other assembly is not available.

    • protected internal

A type or member that is accessible by any code in a class that is derived from its declared assembly or another assembly. access from another assembly must occur in the class declaration, which derives from the class in which the protected inner element is declared, and must occur through an instance of the derived class type.

The following example shows how to specify an access modifier for a type and member:

public class bicycle{Public    void pedal () {}}

Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is limited by the accessibility of its containing type. The following sections provide more detailed information about accessibility.

Accessibility of classes and structs
  • Classes and structs that are declared directly in the namespace (that is, classes and structs that are not nested within other classes or structs) can be public classes and structs, or internal classes and structs. If you do not specify an access modifier, the default is internal.
  • struct members, including nested classes and structs, can be declared as public, internal, or private.   Private nested types cannot be accessed from outside the containing type.
  • b this derives from an internal class a. " In other words, there cannot be a public class  b derived from the inner class  a .  a public, because all protected or internal members of Span Class="code">a is accessible from the derived class. " If this is allowed, the  a  becomes a public class, because all protected members or internal members of  a  can be accessed from derived classes.
  • You can use InternalsVisibleToAttribute to enable some other assemblies to access your internal types.
Accessibility of class members and struct members
  • You can declare class members (including nested classes and structs) using any of the five types of access.   Struct members cannot be declared as protected members because the structure does not support inheritance.
  • Typically, the accessibility of a member is lower than the type of the containing member. If a member implements an interface method or overrides a virtual method that is already defined in a public base class, the public members of the inner class can be accessed from an external assembly.
  • The type of the field, property, or event of any member must be at least as accessible as the member itself.Similarly, the return type and parameter type of any member that is a method, indexer, or delegate must have at least the same accessibility as the member itself.For example, if C is not a public class, you cannot return the public method M of Class C . Similarly, if A is declared private, type a cannot have a protected property.
  • User-defined operators must always be declared as public operators.
  • Destructors cannot have accessibility modifiers.

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

1 //Public class:2  Public classTricycle3 {4     //protected method:5     protected voidpedal () {}6 7     //private field:8     Private intWheels =3;9 Ten     //protected internal property: One     protected Internal intWheels A     { -         Get{returnWheels;} -     } the}
View Code Description
the protected internal accessibility level means protected "or" internal, not protected "and" internal. "  in other words, protected internal members can be accessed from any class within the same assembly, including derived classes. to restrict access to derived classes that are only within the same assembly, declare the class itself as internal and declare its members to be protected.
Other Types
  • when declaring directly with a namespace, you can declare an interface as either a public interface or an internal interface, and the interface defaults to internal accessibility, just like classes and structs.  
  • Delegate behavior is similar to classes and structs. by default, they have internal access when they are declared directly in a namespace, and have private access when nested.

C # access Modifiers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.