zoukankan      html  css  js  c++  java
  • C#修饰符说明

    方法不加访问修饰符默认的是 private
    类不加访问修饰答默认的是 internal
    /////////////////////////////////////////////////////////////
    一个编译器错误提示:在命名空间中定义的元素无法显示的声明为 private, protected, protected internal。
    能在命名空间中定义的元素有:类(class),结构(struct),委托(delegate),接口(interface),枚举(enum) 
    MSDN提示: protected internal 可访问性的意思是受保护“或”内部,而不是受保护“和”内部。
    我们的访问修饰符一共有5个级别
    public, private, protected, internal, protected internal
     
    1.命名空间下的元素的默认访问修饰符
    根据上面的红色错误提示,可以知道命名空间下只能使用两种访问修饰符public和internal。如果没有显示的给这些元素访问修饰符,其修饰符默认为internal 。
    public : 同一程序集的其他任何代码或引用该程序集的其他程序集都可以访问该类型或成员。
    internal : 同一程序集中的任何代码都可以访问该类型或成员,但其他程序集不可以访问。 
     
    2.各类型中的成员的默认访问修饰符
    剩下的修饰符主要是正对继承这个语言特性的,拥有继承的类型有两个类(class)和接口(interface)。public,internal同样可以用于类型成员。
    private : 同一类和结构的代码可以访问该类型和成员。
    protected : 同一类和派生(继承特性)类中的代码可以访问该类型和成员。
    protected internal :  同一程序集中的任何代码或其他程序集中的任何派生类都可以访问该类型或成员。
     
    MSDN提示 :
    1.) 派生类的可访问性不能高于其基类型。换句话说,不能有从内部类 A 派生的公共类 B。如果允许这种情况,将会使 A 成为公共类,因为 A 的所有受保护的成员或内部成员都可以从派生类访问。 
    2.) 成员的可访问性决不能高于其包含类型的可访问性。 
    3.) 可以使用五种访问类型中的任何一种来声明类成员(包括嵌套的类和结构)。 
     
    接口(interface)
    接口成员访问修饰符默认为public,且不能显示使用访问修饰符。
     
    类(class)
    构造函数默认为public访问修饰符。
    析构函数不能显示使用访问修饰符且默认为private访问修饰符。 
    类的成员默认访问修饰符为private; 
     
    枚举(enum)
    枚举类型成员默认为public访问修饰符,且不能显示使用修饰符。
     
    结构(struct) 
    结构成员默认为private修饰符。 
    结构成员无法声明为protected成员,因为结构不支持继承。 
     
    嵌套类型
    嵌套类型的默认访问修饰符为private。 和类,结构的成员默认访问类型一致。
    ///////////////////////////////////////////////////////////////
    c# 的访问修饰符是private 还是 internal?
    准确的说,不能一概而论。
    [MSDN]
    Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.
    类(class)或结构(struct)如果不是在其它类或结构中的话,它的访问类型要不就是internal, 要不就是public;
    换句话说,如果它在其它类或结构中的话,则可以为private 或protected等。下面我说的类和结构,如无特殊说明,均指非"类中类"
    类或结构的默认访问类型是internal.
    类中所有的成员,默认均为private。
    [MSDN]
    Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied.
    Namespaces and enumeration members are always public, and no access modifiers can be applied.
    Delegates have internal access by default.
    Any types declared within a namespace or at the top level of a compilation unit (for example, not within a namespace, class, or struct) are internal by default, but can be made public.
    接口默认访问符是internal
    接口的成员默认访问修饰符是public,也不可能是其他访问修饰符
    命名空间,枚举类型成员默认public,也不可能是其他访问修饰符
    委托,默认internal
    在命名空间内部或编译单元顶部的所有类型,默认是internal,可以人为改为public。
    

      

  • 相关阅读:
    android下socket编程问题:服务器关闭时,客户端发送请求的异常处理
    MySQL新建用户,授权,删除用户,修改密码
    jquery验证表单代码
    Incorrect key file for table '/tmp/#sql_46fd_0.MYI'; try to repair it
    初试百度地图API
    Android控件之GridView探究
    使用Intent调用内置应用程序
    消除SDK更新时的“https://dl-ssl.google.com refused”错误
    A folder failed to be renamed or moved--安装Android SDK的问题
    windows下搭建svn服务器
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/6382546.html
Copyright © 2011-2022 走看看