Accessibility in C # (summary)

Source: Internet
Author: User
Tags modifiers

First, the declared accessibility in C #
1. Public: access is not restricted;
2. Internal: Access is limited to the owning assembly;
3. Protected: Access is limited to the current class and its subclasses;
4. Internal protected: Access is restricted to the current assembly or its subclasses (subclasses may not belong to the current assembly);
5. Private: Access is limited to the type that contains the member.

second, default accessibility
1. The namespace (namespace) implicitly has public access and does not allow access modifiers;
2. The type declared in an edit cell or namespace can have public or internal accessibility, the default is internal;

4. struct (struct) The default access level is private;
6. The default access level for class and struct members (member functions and member variables) is private; (but types declared as namespace members can only have public or internal accessibility);
7. The member of the interface implicitly has public accessibility, and the access modifier is not allowed in the declaration of the interface member;
6. The members of an enumeration implicitly have public accessibility, and access modifiers are not allowed in the declaration.

iii. Accessibility Constraints
1. The direct base class of a class type must be at least as accessible as the class type itself;
2. The base interface of the interface type must be at least as accessible as the interface type itself;
3. The return type and parameter type of the delegate type must be at least as accessible as the delegate type itself;
4. The type of the constant must be at least as accessible as the constant itself;
5. The type of the domain must be at least as accessible as the domain itself;
6. The return type and parameter type of the method must be at least as accessible as the method itself;
7. The type of the property must be at least as accessible as the property itself;
8. The type of event must be at least as accessible as the event itself;
9. The type and parameter type of the indexer must have the same accessibility as the indexer itself;
10. The return type and parameter type of the operator must be at least as accessible as the operator itself;
11. The argument type of the instance constructor must be at least as accessible as the instance constructor itself.

Four, access to the domain
1. A predefined type (for example, object, int, or double) can have unlimited access to the domain;
2. The accessibility domain of the top-level type (not a member declared in a type) declared in program P refers to:
1). If the declared accessibility of T is public, then the accessible domain of T will be p and the program text of any program referencing p;
2). If the declared accessibility of T is internal, then the accessible domain of T will be the program text of P.
visible, the accessible domain of a fixed type is always at least the program text of the program in which the type is declared.
3. A nested member declared in the type T of program P (a member declared in another type) M, whose accessibility domain is one of the following (the M itself may also be a type):
1). If the declared accessibility of M is public, then the accessible domain of M will be the accessible domain of T;
2). If M's declared accessibility is internal protected, set D to represent the program text of P and all the assembly of program text derived from T (those types are declared outside of P), then the accessible domain of M will be the intersection of the accessible domain of T and D;
3). If M's declared accessibility is protected, set D to represent the program text of T and all the set of program text for all types derived from T, then the accessible domain of M will be the intersection of the accessible domain of T and D;
4). If the declared accessibility of M is internal, then the accessible domain of M will be the intersection of the accessible domain of T and the program text of P;
5). If M's declared accessibility is private, the accessible domain of M will be the program text of T.
visible, the accessible domain of a nested member is always at least the program text of the type that declares the member, and will never be greater than the accessible domain of the type that declares the member.
Example
Public class A
{
public static int X;
internal static int Y;
private static int Z;
}
Internal class B
{
public static int X;
internal static int Y;
private static int Z;
Public class C
{
public static int X;
internal static int Y;
private static int Z;
}
Private Class D
{
public static int X;
internal static int Y;
private static int Z;
}
}
in this example, classes and members have the following accessible domains:

1) The accessibility domain of a and a.x is unrestricted;
2) The accessible domain of A.Y, B, b.x, B.y, B.C, b.c.x, and B.c.y is the program text of the program that contains this code;
3) The accessible domain of A.Z is the program text of A;
4) The accessible domain of B.Z and B.D is the program text of B, including the program text of B.C and B.D;
5) The accessible domain of B.C.Z is the program text of B.C;
6) The accessible domain of b.d.x and B.D.Y is the program text of B, including the program text of B.C and B.D;
7) The accessible domain of the B.D.Z is the program text of the B.D.

Example: All members of a base class (except instance constructors, destructors, and static constructors) are inherited by derived types that even include private members of the base class. However, the accessible domain of a private member includes only the program text of the type that declares the member. In the following example, Class B inherits the private member X of Class A:
class A
{
int x;//The default accessibility of a member of a class is private
static void F (b b) {
b.x = 1;//Allow
}
}
class B:a
{
static void F (b b) {
b.x = 1;//error, cannot access x
}
}
because the member is private, it can only be accessed in the class body of a. Therefore, access to b.x in the A.f method succeeds, but access to b.x in the B.f method can cause errors.

Accessibility in C # (summary)

Related Article

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.