“受保护”和“受保护”有什么区别? 和“受保护的内部”?
有人可以解释一下 C# 中 protected
和 protected inside
修饰符之间的区别吗? 看起来他们的行为是相同的。
Can someone please explain the difference between the protected
and protected internal
modifiers in C#? It looks like their behavior is identical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
“受保护的内部”访问修饰符是“受保护”和“内部”修饰符的联合。
来自 MSDN,访问修饰符(C# 编程指南):
< a href="http://msdn.microsoft.com/en-us/library/bcd5672a.aspx" rel="noreferrer">受保护:
内部:
受保护的内部:
请注意:
受保护的内部
表示“受保护
或内部
”(同一程序集中的任何类,或任何派生类)类 - 即使它位于不同的程序集中)。...为了完整起见:
私人 :
公开:
私有保护:
The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers.
From MSDN, Access Modifiers (C# Programming Guide):
protected:
internal:
protected internal:
Note that:
protected internal
means "protected
ORinternal
" (any class in the same assembly, or any derived class - even if it is in a different assembly)....and for completeness:
private:
public:
private protected:
该表显示了差异。
protected inside
与protected
相同,只是它还允许从同一程序集中的其他类进行访问。This table shows the difference.
protected internal
is the same asprotected
, except it also allows access from other classes in the same assembly.protected
可以被任何程序集中的任何子类使用。protected inside
是protected
的所有内容,加上同一程序集中的任何内容都可以访问它。重要的是,它并不意味着“同一程序集中的子类” - 它是两者的并集,而不是交集。
protected
can be used by any subclasses from any assembly.protected internal
is everything thatprotected
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.
在实践中,关于方法:
受保护 - 继承类可访问,否则为私有。
内部 - 仅对程序集中的类公开,否则为私有。
受保护的内部 - 表示受保护的或内部 - 方法可供继承类和程序集中的任何类访问。
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.
尽管大多数访问器的定义都是正确的,但在理解“受保护的内部”访问器的范围方面仍然存在很多混乱。 这帮助我理解了“受保护”和“受保护内部”之间的混淆:
公共实际上是在程序集内部和外部公开的(公共内部/公共外部)
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.
受保护:变量或方法仅可用于子类(在任何程序集中)
受保护内部:可用于任何程序集中的子类和同一程序集中的所有类
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
我已经读出了这些术语的非常清晰的定义。
受保护:访问仅限于类定义内以及从该类继承的任何类。 类型或成员只能由同一类或结构中的代码或从该类派生的类中的代码访问。
内部:访问仅限于当前项目程序集中定义的类。 类型或成员只能由同一类中的代码访问。
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.
将受保护的内部视为在同一字段、属性或方法上应用两个访问修饰符(受保护和内部)。
在现实世界中,想象一下我们正在为人们颁发参观博物馆的特权:
我们可以通过以下方式将它们组合在一起:
编程世界:
内部:该字段在程序集(项目)中随处可用。 这就像说它在其项目范围内是公共的(但不能在项目范围之外访问,即使是程序集外部继承自该类的那些类也是如此)。 该类型的每个实例都可以在该程序集(项目范围)中看到它。
受保护:仅意味着所有派生类都可以看到它(程序集内部或外部)。 例如,派生类可以使用以下命令查看其方法和构造函数内的字段或方法:
base.NameOfProtectedInternal
。因此,将这两个访问修饰符放在一起(
受保护的内部
),您就拥有了可以在项目内部公开的东西,并且可以通过那些在其范围内从该类继承的内容。Think about
protected internal
as applying two access modifier (protected
, andinternal
) on the same field, property or method.In the real world, imagine we are issuing privilege for people to visit museum:
And we can put them together in these way:
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.受保护成员
类的受保护成员仅在所包含的类(已在其中声明它)以及程序集内部和程序集外部的派生类中可用。
意味着驻留在程序集外部的类只能通过继承该类来使用另一个程序集的受保护成员。
我们可以通过继承该类在程序集外部公开受保护的成员,并仅在派生类中使用它。
内部成员
类的内部成员可以在程序集中创建对象或在派生类中使用或访问,也可以说它可以在程序集中的所有类中访问。
受保护的内部
受保护的内部访问修饰符是“受保护”或“内部”的组合。
受保护的内部成员可以在声明创建对象或通过继承该类的整个程序集中可用。 并且只能在派生类的程序集外部进行访问。
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.
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 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.
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.
当您希望在另一个程序集的派生类中使用成员或类型,同时只想使用父程序集中的成员或类型而不从声明它的类派生时,受保护的内部最佳套件。
此外,如果您只想使用成员或类型而不从另一个类派生,则在同一程序集中只能使用内部。
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.
此描述可能会有所帮助
内部成员
内部
类
的成员在程序集中可用或可访问
要么创建对象,要么在派生的类
中,或者您可以说它可以在程序集
内的所有类
之间访问。受保护成员
受保护
类
的成员仅在所包含的类
中可用(它已在其中声明)以及在程序集
内以及程序集
之外的派生类
中。Protected Internal
Protected Internal
访问修饰符是Protected
或Internal
的组合。受保护的内部
成员可以在整个程序集
中使用,其中它声明创建对象
或通过继承该类
。 并且只能在派生类
中的程序集
外部进行访问。This description might be helpful
Internal Member
Internal
Member of aclass
is available or access within theassembly
either creating object or in a derivedclass
or you can say it is accessible across all theclasses
within theassembly
.Protected Member
Protected
Member of aclass
in only available in the containedclass
(in which it has been declared) and in the derivedclass
within theassembly
and also outside theassembly
.Protected Internal
Protected Internal
access modifier is combinationProtected
orInternal
.Protected Internal
Member can be available within the entireassembly
in which it declared either creatingobject
or by inherited thatclass
. And can be accessible outside theassembly
in a derivedclass
only.