类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?


当前回答

最简单的答案是.....

c#中的所有成员默认都使用LEAST accessible修饰符。

这就是为什么程序集中的所有顶级类默认情况下都是“内部”的,这意味着它们对于所处的程序集是公共的,但对于外部程序集是私有的或排除在外的。顶级类的唯一其他选项是public,它更容易访问。对于嵌套类型,除了少数罕见的例外,如枚举成员和接口,它都是私有的,只能是公共的。一些例子。在顶级类和接口的情况下,默认值是:

class Animal与内部类Animal相同

interface动物与公共接口相同

在嵌套类和接口(内部类型)的情况下,默认值为:

class Animal与private class Animal相同

interface动物与私有接口动物相同

如果您只是假设c#类型的默认值总是最私密的,那么在需要更改默认值之前,您不需要使用自定义访问器。他们在箱子外面很安全!

其他回答

Internal是默认的修饰符

c#中所有东西的默认访问权限是“你可以为该成员声明的最受限制的访问权限”。

例如:

namespace MyCompany
{
    class Outer
    {
        void Foo() {}
        class Inner {}
    }
}

等于

namespace MyCompany
{
    internal class Outer
    {
        private void Foo() {}
        private class Inner {}
    }
}

这种情况的一个例外是使属性的一部分(通常是setter)比属性本身声明的可访问性更受限制:

public string Name
{
    get { ... }
    private set { ... } // This isn't the default, have to do it explicitly
}

这是c# 3.0规范必须说的(第3.5.1节):

Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility. Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations. Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility. Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations. Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.

(请注意,嵌套类型将出现在“类成员”或“结构成员”部分-因此默认为私有可见性。)

简而言之:尽可能少的访问(参见Jon Skeet的回答)。

长一点的回答:

非嵌套类型、枚举和委托可访问性(可能只有内部或公共可访问性)

|默认|允许声明的访问权限 ------------------------------------------------------------------ 命名空间| public | none(总是隐式公共) Enum | public | public,内部 接口| internal | public, internal 类| internal | public, internal Struct | internal | public, internal 委托|内部|公共,内部

嵌套类型和成员可访问性

| Default | Permitted declared accessibilities ------------------------------------------------------------------ namespace | public | none (always implicitly public) enum | public | All¹ interface | public | All¹ class | private | All¹ struct | private | public, internal, private² delegate | private | All¹ constructor | private | All¹ enum member | public | none (always implicitly public) interface member | public | none (always implicitly public) method | private | All¹ field | private | All¹ user-defined operator| none | public (must be declared public) ¹ All === public, protected, internal, private, protected internal ² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

嵌套类型的可访问性取决于其可访问域,该域由成员声明的可访问性和直接包含类型的可访问性域共同决定。但是,嵌套类型的可访问域不能超过包含类型的可访问域。

注意:CIL还提供了受保护的和内部的(与现有的受保护的”或“内部的”相对),但据我所知,这在c#中目前是不可用的。


See:

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx http://msdn.microsoft.com/en-us/library/ms173121.aspx http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx (我喜欢微软的网址…)

最简单的答案是.....

c#中的所有成员默认都使用LEAST accessible修饰符。

这就是为什么程序集中的所有顶级类默认情况下都是“内部”的,这意味着它们对于所处的程序集是公共的,但对于外部程序集是私有的或排除在外的。顶级类的唯一其他选项是public,它更容易访问。对于嵌套类型,除了少数罕见的例外,如枚举成员和接口,它都是私有的,只能是公共的。一些例子。在顶级类和接口的情况下,默认值是:

class Animal与内部类Animal相同

interface动物与公共接口相同

在嵌套类和接口(内部类型)的情况下,默认值为:

class Animal与private class Animal相同

interface动物与私有接口动物相同

如果您只是假设c#类型的默认值总是最私密的,那么在需要更改默认值之前,您不需要使用自定义访问器。他们在箱子外面很安全!

类默认为内部。

默认情况下,类成员是私有的。

接口默认为“Internal”。

缺省情况下,接口成员是公共的。(接口不允许 我们不能为它的成员指定任何类型的可访问性。) 注意:如果你试图为接口的成员指定任何访问说明符,它会显示编译错误。

Struct默认为Internal。

默认情况下,结构成员是私有的。