Accessibility of members, membership assembly, static class, member static
Accessibility of members
CLR terminology |
C # terminology |
Description |
Private |
Private |
Members can only be accessed by methods in the definition type or any mosaic type. |
Family |
Protected |
A member can only be accessed by defining a type, any mosaic type, or by a method of any derived type in any assembly |
Family and Assembly |
(Not supported) |
Members can only be accessed by methods of the defined type, any mosaic type, or any derived type defined in the same assembly. |
Assembly |
Internal |
Members can only be accessed by methods in the definition assembly |
Family or Assembly |
Protected internal |
Members can be accessed by any nested type, any derived type (no matter what assembly), or any method in the definition assembly. |
Public |
Public |
Members can be accessed by any method of any assembly |
If the accessibility of declared members is not displayed, the compiler usually chooses private by default. CRL requires that all members of the interface type have public access.
When a derived class overrides a member defined in its base type, the c # compiler requires that the original and override members have the same accessibility. (CRL not required)
To be accessed by others, any member must be defined in a visible type. If assembly A defines an internal type and has A public method, assembly B cannot access this public method.
Youyuan assembly
You meta-assembly applies to the internal type that can access another definition type.
You can use system to build an assembly. The Runtime. CompilerService namespace defines an attribute named InternalsVisibleTo to indicate other assemblies that he considers as "friends. This attribute is used to obtain a string parameter.
It identifies the name and public key of the friend assembly. After an assembly confirms its membership assembly, those membership assembly will be able to access all the internal types in the Assembly and the internal members of these types.
The following example shows how to specify two strongly-named assembly "Wintellect" and "Microsoft" as their friend assembly.
using System
Using System. runtime. compilerServices; // For the IntrenalVisibleTo attribute // The internal type in the current assembly can be accessed by any code in the two assembly sets (no matter what version or language culture) [assembly: InternalsVisibleTo ("Wintellect, publicKey = 1111 ")] [assembly: InternalsVisibleTo (" Microsoft, PublicKey = 2222 ")] internal sealed class SomeInternalType {} internal sealed class AnotherInternalType {}
// The following is how to access the SomeInternalType of the internal type of the above assembly in the "Wintellect", a friend assembly whose public key is 2222.
using System
Internal sealed class Foo {private static Object SomeMethod () {// This "Wintellect" assembly can access the internal type of another assembly, it's like SomeInternalType sit = new SomeInternalType (); return sit ;}}
Static class
There are some classes that never need to be instantiated, such as the Console, Math, Environment, and ThreadPool classes. These classes only have static members. In fact, the only purpose of this category is to combine a group of relevant members, such
Math class defines a set of methods for performing mathematical operations. In c #, use static to define classes that cannot be instantiated (static cannot be used for struct)
C # The following restrictions are imposed on static classes:
Static classes are not allowed to be derived directly from System objects. It does not make sense to derive from any other base classes. Inheritance only applies to objects, but cannot create instances of static classes.
The static class cannot implement any interface, because only the class instance can call the interface method of the class.
Static classes can only define static members. Any instance Member soy milk causes the Mutator to report an error.
Static classes cannot be used as fields, method parameters, or local variables, because they all represent referencing an instance variable. Otherwise, the compiler will report an error when detecting any such usage.