“受保护”和“受保护”有什么区别? 和“受保护的内部”?

发布于 2024-07-14 02:47:54 字数 101 浏览 9 评论 0原文

有人可以解释一下 C# 中 protectedprotected inside 修饰符之间的区别吗? 看起来他们的行为是相同的。

Can someone please explain the difference between the protected and protected internal modifiers in C#? It looks like their behavior is identical.

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

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

发布评论

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

评论(12

二货你真萌 2024-07-21 02:47:54

“受保护的内部”访问修饰符是“受保护”和“内部”修饰符的联合。

来自 MSDN,访问修饰符(C# 编程指南)

< a href="http://msdn.microsoft.com/en-us/library/bcd5672a.aspx" rel="noreferrer">受保护

该类型或成员只能由同一类或同一类中的代码访问
结构体,或在从该类派生的类中。

内部

同一程序集中的任何代码都可以访问类型或成员,
但不是来自另一个程序集。

受保护的内部

该类型或成员可以由程序集中的任何代码访问
它是从另一个派生类中声明的,
集会。 从另一个程序集的访问必须在类中进行
派生自受保护的类的声明
声明内部元素,并且必须通过
派生类类型的实例。

请注意受保护的内部表示“受保护内部”(同一程序集中的任何类,或任何派生类)类 - 即使它位于不同的程序集中)。

...为了完整起见:

私人 :

该类型或成员只能由同一类或同一类中的代码访问
结构体。

公开

该类型或成员可以由同一区域中的任何其他代码访问
程序集或引用它的另一个程序集。

私有保护

访问仅限于包含类或从派生的类型
当前程序集中包含类。
自 C# 7.2 起可用

The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers.

From MSDN, Access Modifiers (C# Programming Guide):

protected:

The type or member can be accessed only by code in the same class or
struct, or in a class that is derived from that class.

internal:

The type or member can be accessed by any code in the same assembly,
but not from another assembly.

protected internal:

The type or member can be accessed by any code in the assembly in
which it is declared, OR from within a derived class in another
assembly. Access from another assembly must take place within a class
declaration that derives from the class in which the protected
internal element is declared, and it must take place through an
instance of the derived class type.

Note that: protected internal means "protected OR internal" (any class in the same assembly, or any derived class - even if it is in a different assembly).

...and for completeness:

private:

The type or member can be accessed only by code in the same class or
struct.

public:

The type or member can be accessed by any other code in the same
assembly or another assembly that references it.

private protected:

Access is limited to the containing class or types derived from the
containing class within the current assembly.
(Available since C# 7.2)

又怨 2024-07-21 02:47:54

该表显示了差异。 protected insideprotected 相同,只是它还允许从同一程序集中的其他类进行访问。

C# 比较修饰符

This table shows the difference. protected internal is the same as protected, except it also allows access from other classes in the same assembly.

Comparison of C# modifiers

夜灵血窟げ 2024-07-21 02:47:54

protected 可以被任何程序集中的任何子类使用。

protected insideprotected 的所有内容,加上同一程序集中的任何内容都可以访问它。

重要的是,它并不意味着“同一程序集中的子类” - 它是两者的并集,而不是交集。

protected can be used by any subclasses from any assembly.

protected internal is everything that protected is, plus also anything in the same assembly can access it.

Importantly, it doesn't mean "subclasses in the same assembly" - it is the union of the two, not the intersection.

予囚 2024-07-21 02:47:54

在实践中,关于方法:

受保护 - 继承类可访问,否则为私有。

内部 - 仅对程序集中的类公开,否则为私有。

受保护的内部 - 表示受保护的内部 - 方法可供继承类程序集中的任何类访问。

In practice, about methods:

protected - accessible for inherited classes, otherwise private.

internal - public only for classes inside the assembly, otherwise private.

protected internal - means protected or internal - methods become accessible for inherited classes and for any classes inside the assembly.

浮华 2024-07-21 02:47:54

尽管大多数访问器的定义都是正确的,但在理解“受保护的内部”访问器的范围方面仍然存在很多混乱。 这帮助我理解了“受保护”和“受保护内部”之间的混淆:

公共实际上是在程序集内部和外部公开的(公共内部/公共外部

protected 在程序集内部和外部确实受到保护(受保护的内部/受保护的外部)(顶级类不允许)

private 在程序集内部和外部确实是私有的程序集(私有内部/私有外部)(顶级类不允许)

内部在程序集中确实是公共的,但像私有(公共内部/排除外部

受保护的内部在程序集内部确实是公共的,但在程序集外部受到保护(公共内部/受保护的外部
(顶级课程不允许)

如您所见,受保护的内部是一个非常奇怪的野兽。 不直观。

现在这就引出了一个问题,为什么微软不创建一个(受保护的内部/排除的外部),或者我猜某种“私有保护”或“内部保护”? 哈哈。 看起来不完整?

更令人困惑的是,您可以将公共或受保护的内部嵌套成员嵌套在受保护、内部或私有类型中。 为什么要访问排除外部程序集访问的内部类内的嵌套“受保护的内部”?

微软表示,此类嵌套类型受到其父类型范围的限制,但这并不是编译器所说的。 您可以在内部类中编译受保护的内部结构,这应该将范围限制为程序集。

对我来说,这感觉像是不完整的设计。 他们应该将所有类型的范围简化为一个明确考虑继承以及嵌套类型的安全性和层次结构的系统。 这将使对象的共享变得极其直观和精细,而不是基于不完整的范围系统来发现类型和成员的可访问性。

There is still a lot of confusion in understanding the scope of "protected internal" accessors, though most have the definition defined correctly. This helped me to understand the confusion between "protected" and "protected internal":

public is really public inside and outside the assembly (public internal / public external)

protected is really protected inside and outside the assembly (protected internal / protected external) (not allowed on top level classes)

private is really private inside and outside the assembly (private internal / private external) (not allowed on top level classes)

internal is really public inside the assembly but excluded outside the assembly like private (public internal / excluded external)

protected internal is really public inside the assembly but protected outside the assembly (public internal / protected external)
(not allowed on top level classes)

As you can see protected internal is a very strange beast. Not intuitive.

That now begs the question why didn't Microsoft create a (protected internal / excluded external), or I guess some kind of "private protected" or "internal protected"? lol. Seems incomplete?

Added to the confusion is the fact you can nest public or protected internal nested members inside protected, internal, or private types. Why would you access a nested "protected internal" inside an internal class that excludes outside assembly access?

Microsoft says such nested types are limited by their parent type scope, but that's not what the compiler says. You can compiled protected internals inside internal classes which should limit scope to just the assembly.

To me this feels like incomplete design. They should have simplified scope of all types to a system that clearly consider inheritance but also security and hierarchy of nested types. This would have made the sharing of objects extremely intuitive and granular rather than discovering accessibility of types and members based on an incomplete scoping system.

南冥有猫 2024-07-21 02:47:54

受保护:变量或方法仅可用于子类(在任何程序集中)

受保护内部:可用于任何程序集中的子类同一程序集中的所有类

protected: the variable or method will be available only to child classes (in any assembly)

protected internal: available to child classes in any assembly and to all the classes within the same assembly

这样的小城市 2024-07-21 02:47:54

我已经读出了这些术语的非常清晰的定义。

受保护:访问仅限于类定义内以及从该类继承的任何类。 类型或成员只能由同一类或结构中的代码或从该类派生的类中的代码访问。

内部:访问仅限于当前项目程序集中定义的类。 类型或成员只能由同一类中的代码访问。

Protected-Internal :访问仅限于当前程序集或从包含类派生的类型。

I have read out very clear definitions for these terms.

Protected : Access is limited to within the class definition and any class that inherits from the class. The type or member can be accessed only by code in the same class or struct or in a class that is derived from that class.

Internal : Access is limited to exclusively to classes defined within the current project assembly. The type or member can be accessed only by code in same class.

Protected-Internal : Access is limited to current assembly or types derived from containing class.

极致的悲 2024-07-21 02:47:54

将受保护的内部视为在同一字段、属性或方法上应用两个访问修饰符(受保护和内部)。

在现实世界中,想象一下我们正在为人们颁发参观博物馆的特权:

  1. 城市内的每个人都可以参观博物馆(内部)。
  2. 父母居住的城市以外的每个人都可以参观博物馆(受保护)。

我们可以通过以下方式将它们组合在一起:

城市(内部)内部的每个人和其父母居住在这里(受保护)的城市外部的每个人都可以参观博物馆(受保护的内部< /strong>)。


编程世界:

内部:该字段在程序集(项目)中随处可用。 这就像说它在其项目范围内是公共的(但不能在项目范围之外访问,即使是程序集外部继承自该类的那些类也是如此)。 该类型的每个实例都可以在该程序集(项目范围)中看到它。

受保护:仅意味着所有派生类都可以看到它(程序集内部或外部)。 例如,派生类可以使用以下命令查看其方法和构造函数内的字段或方法:base.NameOfProtectedInternal

因此,将这两个访问修饰符放在一起(受保护的内部),您就拥有了可以在项目内部公开的东西,并且可以通过那些在其范围内从该类继承的内容。

它们可以写成internal protected,并且改变含义,但是写成protectedinternal比较方便。

Think about protected internal as applying two access modifier (protected, and internal) on the same field, property or method.

In the real world, imagine we are issuing privilege for people to visit museum:

  1. Everyone inside the city are allowed to visit museum (internal).
  2. Everyone outside of the city that their parents live here are allowed to visit museum (protected).

And we can put them together in these way:

Everyone inside the city (internal) and everyone outside of city that their parents live here (protected) are allowed to visit the museum (protected internal).

Programming world:

internal: The field is available everywhere in the assembly (project). It is like saying it is public in its project scope (but can not being accessed outside of project scope even by those classes outside of assembly which inherit from that class). Every instance of that type can see it in that assembly (project scope).

protected: simply means that all derived classes can see it (inside or outside of assembly). For example derived classes can see the field or method inside its methods and constructors using: base.NameOfProtectedInternal.

So, putting these two access modifier together (protected internal), you have something that can being public inside the project, and can be seen by those which have inherited from that class inside their scope.

They can be written in the internal protected, and does not change the meaning, but it is convenient to write it protected internal.

烟─花易冷 2024-07-21 02:47:54

受保护成员

类的受保护成员仅在所包含的类(已在其中声明它)以及程序集内部和程序集外部的派生类中可用。

意味着驻留在程序集外部的类只能通过继承该类来使用另一个程序集的受保护成员。

我们可以通过继承该类在程序集外部公开受保护的成员,并仅在派生类中使用它。

注意:无法使用派生类中的对象访问受保护的成员。

内部成员

类的内部成员可以在程序集中创建对象或在派生类中使用或访问,也可以说它可以在程序集中的所有类中访问。

注意:无论是使用对象创建还是在派生类中,都无法在程序集外部访问内部成员。

受保护的内部

受保护的内部访问修饰符是“受保护”或“内部”的组合。

受保护的内部成员可以在声明创建对象或通过继承该类的整个程序集中可用。 并且只能在派生类的程序集外部进行访问。

注意:受保护的内部成员在同一程序集中用作内部成员,并在程序集外部用作受保护的成员。

Protected Member

Protected Member of a class in only available in the contained class (in which it has been declared) and in the derived class within the assembly and also outside the assembly.

Means if a class that resides outside the assembly can use the protected member of the other assembly by inherited that class only.

We can exposed the Protected member outside the assembly by inherited that class and use it in the derived class only.

Note: Protected members are not accessible using the object in the derived class.

Internal Member

Internal Member of a class is available or access within the assembly either creating object or in a derived class or you can say it is accessible across all the classes within the assembly.

Note: Internal members not accessible outside the assembly either using object creating or in a derived class.

Protected Internal

Protected Internal access modifier is combination Protected or Internal.

Protected Internal Member can be available within the entire assembly in which it declared either creating object or by inherited that class. And can be accessible outside the assembly in a derived class only.

Note: Protected Internal member works as Internal within the same assembly and works as Protected for outside the assembly.

你的背包 2024-07-21 02:47:54

public - 声明为 public 的成员(函数和变量)可以从任何地方访问。

私有 - 无法从类外部访问私有成员。 这是成员的默认访问说明符,即如果您没有为成员(变量或函数)指定访问说明符,它将被视为私有。 因此,字符串 PhoneNumber; 相当于私有字符串PhoneNumber。

受保护 - 受保护的成员只能从子类访问。

内部 - 只能在同一程序集中访问。

受保护的内部 - 可以在同一程序集中以及派生类中访问它。

public - The members (Functions & Variables) declared as public can be accessed from anywhere.

private - Private members cannot be accessed from outside the class. This is the default access specifier for a member, i.e if you do not specify an access specifier for a member (variable or function), it will be considered as private. Therefore, string PhoneNumber; is equivalent to private string PhoneNumber.

protected - Protected members can be accessed only from the child classes.

internal - It can be accessed only within the same assembly.

protected internal - It can be accessed within the same assembly as well as in derived class.

得不到的就毁灭 2024-07-21 02:47:54

当您希望在另一个程序集的派生类中使用成员或类型,同时只想使用父程序集中的成员或类型而不从声明它的类派生时,受保护的内部最佳套件。
此外,如果您只想使用成员或类型而不从另一个类派生,则在同一程序集中只能使用内部。

Protected internal best suites when you want a member or type to be used in a derived class from another assembly at the same time just want to consume the member or type in the parent assembly without deriving from the class where it is declared.
Also if you want only to use a member or type with out deriving from another class, in the same assembly you can use internal only.

盗心人 2024-07-21 02:47:54

此描述可能会有所帮助

内部成员

内部 的成员在程序集中可用或可访问要么创建对象,要么在派生的中,或者您可以说它可以在程序集内的所有之间访问。

受保护成员

受保护的成员仅在所包含的中可用(它已在其中声明)以及在程序集内以及程序集之外的派生中。

Protected Internal

Protected Internal 访问修饰符是 ProtectedInternal 的组合。

受保护的内部成员可以在整个程序集中使用,其中它声明创建对象或通过继承该 。 并且只能在派生中的程序集外部进行访问。

This description might be helpful

Internal Member

Internal Member of a class is available or access within the assembly either creating object or in a derived class or you can say it is accessible across all the classes within the assembly.

Protected Member

Protected Member of a class in only available in the contained class (in which it has been declared) and in the derived class within the assembly and also outside the assembly.

Protected Internal

Protected Internal access modifier is combination Protected or Internal.

Protected Internal Member can be available within the entire assembly in which it declared either creating object or by inherited that class. And can be accessible outside the assembly in a derived class only.

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