C# 中默认的访问修饰符是什么?

发布于 2024-08-26 20:02:02 字数 37 浏览 7 评论 0原文

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

What is the default access modifier for classes, methods, members, constructors, delegates and interfaces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

英雄似剑 2024-09-02 20:02:02

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):

取决于上下文
仅发生成员声明
某些类型的声明
允许访问。
此外,当成员声明
不包含任何访问修饰符,
声明的上下文
发生决定默认值
声明可访问性。

  • 命名空间隐式具有公开声明的可访问性。无法访问
    命名空间允许使用修饰符
    声明。
  • 在编译单元或命名空间中声明的类型可以具有 public 或
    内部声明的可访问性和
    默认为内部声明
    无障碍。
  • 类成员可以具有五种声明的可访问性中的任何一种
    并默认为私有声明
    可达性。 (请注意,一个类型
    声明为类的成员可以
    具有五种声明中的任何一种
    可访问性,而声明的类型
    作为命名空间的成员可以有
    仅公开或内部声明
    无障碍。)
  • 结构体成员可以声明为公共、内部或私有
    可访问性并默认为私有
    声明可访问性,因为结构
    被隐式密封。结构成员
    在结构体中引入(也就是说,不是
    由该结构继承)不能有
    受保护或受保护内部
    声明的可访问性。 (请注意,一个
    声明为结构体成员的类型
    可以有公共的、内部的或私有的
    声明了可访问性,而类型
    声明为命名空间的成员
    只能有公共或内部
    声明可访问性。)
  • 接口成员隐式具有公开声明的可访问性。不
    允许访问修饰符
    接口成员声明。
  • 枚举成员隐式具有公开声明的可访问性。不
    允许访问修饰符
    枚举成员声明。

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

The default access for everything in C# is "the most restricted access you could declare for that member".

So for example:

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

is equivalent to

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

The one sort of exception to this is making one part of a property (usually the setter) more restricted than the declared accessibility of the property itself:

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

This is what the C# 3.0 specification has to say (section 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.

(Note that nested types would come under the "class members" or "struct members" parts - and therefore default to private visibility.)

浊酒尽余欢 2024-09-02 20:02:02
top level class: internal
method: private
members (unless an interface or enum): private (including nested classes)
members (of interface or enum): public
constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
delegate: internal
interface: internal
explicitly implemented interface member: public!
top level class: internal
method: private
members (unless an interface or enum): private (including nested classes)
members (of interface or enum): public
constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
delegate: internal
interface: internal
explicitly implemented interface member: public!
痞味浪人 2024-09-02 20:02:02

简短回答:最小可能的访问权限(参见 Jon Skeet 的回答)。

长答案:

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

<前> <代码> |默认|允许声明的可访问性
-------------------------------------------------- ----------------
命名空间|公共|无(始终隐式公开)

枚举 |公共|公共的、内部的

接口|内部|公共的、内部的

类 |内部|公共的、内部的

结构 |内部|公共的、内部的

代表|内部|公共的、内部的

嵌套类型和成员可访问性

<前> <代码> |默认|允许声明的可访问性
-------------------------------------------------- ----------------
命名空间|公共|无(始终隐式公开)

枚举 |公共|全部 1

接口|公共|全部 1

类 |私人|全部 1

结构 |私人|公共、内部、私人²

代表|私人|全部 1

构造函数|私人|全部 1

枚举成员 |公共|无(始终隐式公开)

接口成员|公共|无(始终隐式公开)

方法|私人|全部 1

领域 |私人|全部 1

用户定义运算符|无 |公开(必须声明为公开)

全部 === 公共、受保护、内部、私有、受保护内部

² 结构不能从结构或类继承(尽管可以从接口继承),因此 protected 不是有效的修饰符

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

注意:CIL 还提供了受保护和内部的规定(与现有的受保护的“或”内部相反),但据我所知,目前在 C# 中无法使用。


请参阅:

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
(我喜欢微软的 URL...)

Short answer: minimum possible access (cf Jon Skeet's answer).

Long answer:

Non-nested types, enumeration and delegate accessibilities (may only have internal or public accessibility)

                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | public, internal

interface            | internal  | public, internal

class                | internal  | public, internal

struct               | internal  | public, internal

delegate             | internal  | public, internal

Nested type and member accessiblities

                     | 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

The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

Note: CIL also has the provision for protected and internal (as opposed to the existing protected "or" internal), but to my knowledge this is not currently available for use in 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
(Man I love Microsoft URLs...)

春庭雪 2024-09-02 20:02:02

看看 访问修饰符(C# 编程指南)

类和结构的可访问性

直接在命名空间内声明的类和结构(换句话说,未嵌套在其他类或结构中)可以是公共的或内部的。如果未指定访问修饰符,则默认为 Internal。

结构成员(包括嵌套类和结构)可以声明为公共、内部或私有。类成员(包括嵌套类和结构)可以是公共成员、受保护内部成员、受保护内部成员、私有受保护成员或私有成员。类成员和结构成员(包括嵌套类和结构)的访问级别默认为私有。私有嵌套类型无法从包含类型的外部访问。

派生类不能比其基类型具有更高的可访问性。换句话说,您不能拥有从内部类 A 派生的公共类 B。如果允许这样做,它将具有使 A 成为公共的效果,因为 A 的所有受保护或内部成员都可以从派生类访问。< /p>

您可以使用 InternalsVisibleToAttribute 启用特定的其他程序集来访问您的内部类型。有关详细信息,请参阅友元程序集。

类和结构成员可访问性

可以使用六种访问类型中的任何一种来声明类成员(包括嵌套类和结构)。结构体成员不能声明为受保护,因为结构体不支持继承。

通常,成员的可访问性不会大于包含该成员的类型的可访问性。但是,如果内部类的公共成员实现接口方法或重写公共基类中定义的虚拟方法,则可以从程序集外部访问该成员。

作为字段、属性或事件的任何成员的类型必须至少与成员本身一样可访问。同样,作为方法、索引器或委托的任何成员的返回类型和参数类型必须至少与成员本身一样可访问。例如,除非 C 也是公共的,否则您不能拥有返回类 C 的公共方法 M。同样,如果 A 被声明为私有,则您不能拥有 A 类型的受保护属性。

用户定义的运算符必须始终声明为公共和静态。有关详细信息,请参阅运算符重载。

终结器不能具有辅助功能修饰符。

其他类型

直接在命名空间内声明的接口可以声明为公共或内部,就像类和结构一样,接口默认为内部访问。接口成员始终是公共的,因为接口的目的是使其他类型能够访问类或结构。不能对接口成员应用访问修饰符。

枚举成员始终是公共的,并且不能应用访问修饰符。

委托的行为类似于类和结构。默认情况下,直接在命名空间内声明时它们具有内部访问权限,而嵌套时则具有私有访问权限。

Have a look at Access Modifiers (C# Programming Guide)

Class and Struct Accessibility

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies.

Class and Struct Member Accessibility

Class members (including nested classes and structs) can be declared with any of the six types of access. Struct members cannot be declared as protected because structs do not support inheritance.

Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

The type of any member that is a field, property, or event must be at least as accessible as the member itself. Similarly, the return type and the parameter types of any member that is a method, indexer, or delegate must be at least as accessible as the member itself. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private.

User-defined operators must always be declared as public and static. For more information, see Operator overloading.

Finalizers cannot have accessibility modifiers.

Other Types

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

Enumeration members are always public, and no access modifiers can be applied.

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.

黎夕旧梦 2024-09-02 20:02:02

默认情况下,类是内部

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

默认情况下,接口是内部

  • 默认情况下,接口成员是公开的。 (接口不允许
    我们指定其成员的任何类型的可访问性。)

    注意:如果您尝试为接口的成员指定任何访问说明符,则会显示编译错误。

默认情况下,结构是内部

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

Class is Internal by default.

  • Class members are private by default.

Interface is Internal by default.

  • Interface members are public by default. (Interfaces won't allow
    us to specify any kind of accessibility to it's members.)

    Note: If you try to specify any access specifier to interface's members then, it shows compile error.

Struct is Internal by default.

  • Struct members are private by default.
无法回应 2024-09-02 20:02:02

我想添加一些文档链接。请在此处查看更多详细信息。

输入图像描述这里

I would like to add some documentation link. Check out more detail here.

enter image description here

|煩躁 2024-09-02 20:02:02

最简单的答案如下...

默认情况下,C# 中的所有成员始终采用可能的 LEAST 可访问修饰符。

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

中有两类访问修饰符,

在 C#顶级...

class MyClass {
    ...
}

和成员级(在另一个类中等)

class MyClass {
    class MySubClass {
        ...
    }
}

每种访问修饰符都有自己不同的默认值。一般来说,所有顶层都可以是internal(默认,与无访问修饰符相同,并且阻止外部程序集访问)或public(对程序集和外部程序集访问开放) 。

顶级类型访问修饰符

对于顶级类、结构、接口等,默认情况下都是内部,如使用“animal”类所示举个例子。这意味着它们默认不对外部程序集访问:

class Animal{}internal class Animal{}< /strong>

interface Animal{}internal interface Animal{}

成员级别类型访问修饰符

对于嵌套类和接口(内部类型),默认值通常是 private,除了接口和枚举是 public 之外。这通常意味着类中的成员无法从内部程序集及其内部和外部程序集对类成员进行外部访问:

类 Animal{} 与 private class Animal{} 相同

interface Animal{}public 接口 Animal{}

了解您的顶级类型还控制它们内部的成员。因此,如果将顶级访问修饰符internal更改为public,则可以突然访问类内的任何公共成员或受保护的派生成员由外部集会!

请记住,当从控制可访问性的基类派生时,这些规则也可能被打破。但是,如果您只是假设 C# 类型的默认值始终是最私有的,那么在需要更改默认值之前,您不需要使用自定义访问修饰符。它们开箱即用,非常安全!

Simplest answer is the following.....

All members in C# always take the LEAST accessible modifier possible by default.

That is why all top level classes in an assembly are "internal" by default, which means they are public to the assembly they are in, but private or excluded from access to outside assemblies. The only other option for a top level class is public which is more accessible. For nested types its all private except for a few rare exceptions like members of enums and interfaces which can only be public. Some examples.

There are TWO CATEGORIES OF ACCESS MODIFIERS in C#

Top-Level...

class MyClass {
    ...
}

...and Member-Level (inside another class, etc)

class MyClass {
    class MySubClass {
        ...
    }
}

Each has their own defaults that are different. In general all top level can be internal (the default, same as no access modifier, and prevented from outside assembly access) or public (open to assembly and outside assembly access).

TOP-LEVEL TYPE ACCESS MODIFIERS

In the case of top level classes, structs, interfaces, etc. the defaults are all internal by default, as seen using the "animal" class as an example. This means they are closed to external assembly access by default:

class Animal{} is the same as internal class Animal{}

interface Animal{} is the same as internal interface Animal{}

MEMBER-LEVEL TYPE ACCESS MODIFIERS

In the case of nested classes and interfaces (inside types), the defaults are usually private except for interfaces and enums which are public. This generally means members in classes are excluded from both outside access to the class members from both the internal assembly its in and outside assemblies:

class Animal{} is the same as private class Animal{}

interface Animal{} is the same as public interface Animal{}

Understand that your top-level types also control the members inside them. So if you change a top-level access modifier from internal to public, any public members or protected derived ones inside the class can suddenly be accessed by outside assemblies!

Keep in mind these rules can be broken when deriving from base classes which control accessibility, too. But if you just assume the default on C# types is always the most private, then you do not need to use custom access modifiers until you need to change the default. They are secure out of the box!

久隐师 2024-09-02 20:02:02

命名空间级别:internal

类型级别:private

Namespace level: internal

Type level: private

鱼窥荷 2024-09-02 20:02:02

internal 是类的默认访问修饰符。

private 是类成员的默认访问修饰符。

internal is default access modifier for class.

private is default access modifier for class members.

走野 2024-09-02 20:02:02

Internal 是默认修饰符

Internal is the default modifier

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文