首页 技术 正文
技术 2022年11月15日
0 收藏 563 点赞 2,334 浏览 2429 个字

http://stackoverflow.com/questions/22940317/protected-vs-protected-internal-again-in-c-sharp

protected means that you can access the member from any subtype (and of course from the declaring type itself). So regardless of where that subtype is, even if it is in another assembly, you will still have access to all protected members.

internal means that you can access the member from any type in the same assembly. So even a completely unrelated class that lives in the same assembly can access the member.

protected internal combines both, meaning that both apply separately. So you can access the member from any subtype, and you can also access the member from any type in the same assembly.

// Assembly 1
class A {
protected int foo;
internal int bar;
protected internal int baz;
}class B : A {} // can access: foo, bar, baz
class C {} // can access: bar, baz protected类型的foo无法被访问,所以protected internal访问范围比protected高// Assembly 2
class D : A {} // can access: foo, baz internal类型的bar无法被访问,所以protected internal访问范围比internal高
class E {} // can access neither

在Assembly1内部

public class A
{
protected int foo;
internal int bar;
protected internal int baz;
} /// <summary>
/// can access: foo, bar, baz
/// </summary>
public class B : A
{
void Method()
{
A a = new A();
//a.foo = 1;
//can not access protected member 'A.foo' via a qualifier of type 'A'
//the qualifier must be of type of B(or derive from it)
a.bar = ;
a.baz = ; B b = new B();
b.foo = ;
b.bar = ;
b.baz = ;
}
} /// <summary>
/// can access: bar, baz
/// </summary>
class C
{
void Method()
{
A a = new A();
//a.foo = 1; A.foo is inaccessible due to its protectionlevel
a.bar = ;
a.baz = ; B b = new B();
//b.foo = 1; A.foo is inaccessible due to its protectionlevel
b.bar = ;
b.baz = ;
}
}

在Assembly2内部,AssemblyB将AssemblyA添加为引用

    /// <summary>
/// can access: foo, baz
/// </summary>
class D : A
{
void Method()
{
A a = new A();
//a.foo = 1;
//can not access protected member 'A.foo' via a qualifier of type 'A'
//the qualifier must be of type of B(or derive from it) //a.bar = 2; //A.bar is inaccessible due to its protection level //a.baz = 3;
//can not access protected member 'A.foo' via a qualifier of type 'A'
//the qualifier must be of type of B(or derive from it) B b = new B();
//b.foo = 1;
//can not access protected member 'A.foo' via a qualifier of type 'B'
//the qualifier must be of type of D(or derive from it) //b.bar = 2; //A.bar is inaccessible due to its protection level //b.baz = 3;
//can not access protected member 'A.foo' via a qualifier of type 'B'
//the qualifier must be of type of D(or derive from it) D d = new D();
d.foo = ;
//d.bar = 2; //A.bar is inaccessible due to its protection level
d.baz = ;
}
} /// <summary>
/// can access neither
/// </summary>
class E
{
void Method()
{
//什么都访问不到
}
}

What is the difference between ‘protected’ and ‘protected internal’?

– Update answer 2019 –

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

Protected vs protected internal (Again) in c#

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297