gpt4 book ai didi

c# - 'protected' 和 'protected internal' 有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 03:29:07 28 4
gpt4 key购买 nike

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

最佳答案

“protected internal”访问修饰符是“protected”和“internal”修饰符的联合

来自 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 内部:

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.

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

...为了完整起见:

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# - 'protected' 和 'protected internal' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/585859/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com