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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/585859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:15:48  来源:igfitidea点击:

What is the difference between 'protected' and 'protected internal'?

c#.netaccess-modifiers

提问by Embedd_Khurja

Can someone please elaborate me the difference between 'protected' and 'protected internal' modifiers in C#? It looks they behave in same manner.

有人可以详细说明 C# 中“受保护”和“受保护的内部”修饰符之间的区别吗?看起来他们的行为方式相同。

采纳答案by M4N

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

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

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

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

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, ORfrom 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 internalmeans "protectedOR internal" (any class in the same assembly, or any derived class - even if it is in a different assembly).

请注意protected internal表示“protectedinternal”(同一程序集中的任何类,或任何派生类 - 即使它位于不同的程序集中)。

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

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

回答by Marc Gravell

protectedcan be used by any subclasses from any assembly.

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

protected internalis everything that protectedis, plus also anything in the same assembly can access it.

protected internal是一切protected,加上同一个程序集中的任何东西都可以访问它。

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

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

回答by abatishchev

In practice, about methods:

在实践中,关于方法:

protected- accessible for inherited classes, otherwise private.

protected- 继承类可以访问,否则是私有的。

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

内部- 仅适用于程序集中的类,否则为私有。

protected internal- means protected orinternal - methods become accessible for inherited classes andfor any classes inside the assembly.

受保护的内部- 意味着受保护内部 - 继承的类和程序集中的任何类都可以访问方法。

回答by abatishchev

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

protected: 变量或方法只对子类可用(在任何程序集中)

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

protected internal:可用于任何程序集中的子类以及同一程序集中的所有类

回答by Piush shukla

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

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

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.

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

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

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

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

internal- 它只能在同一个程序集中访问。

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

protected internal- 它可以在同一个程序集中以及派生类中访问。

回答by Ammar Asjad

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.

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

回答by Mostafa Bouzari

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.

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

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 访问修饰符是 Protected 或 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.

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

回答by Stokely

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

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

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

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

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

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

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

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

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

internal在程序集内部确实是公开的,但像 private 一样被排除在程序集之外(public internal / exclude external

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

protected internal在程序集内部确实是公共的,但在程序集外部受到保护public internal / protected external)(不允许在顶级类中使用)

As you can see protected internalis 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.

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

回答by satishpkumarin

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.

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

回答by Andi AR

- Update answer 2019 -

- 更新答案 2019 -

You can find the difference in below table based accessibility is yes,

您可以发现下表基于可访问性的区别是,

enter image description here

在此处输入图片说明