Protected vs protected internal (Again) in c#
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#的更多相关文章
- c#中的访问修饰符Protected,privet ,public, internal,和internal protected
Protected,privet ,public, internal,和internal protected的区别 Private修饰的,只能值类内部使用,外部不可以使用,子类不能直接访问,但可以通过 ...
- 简述private,protected,public,internal修饰符的访问权限
private:私有成员,在类的内部才可以访问 protected:保护成员,在类的内部和继承类中可以访问 public:公共成员,完全公开,没有访问限制 internal:当前程序集内可以访问
- c# protected public private internal
1 internal 只能在一个项目中引用,不能跨项目引用,只有在同一程序集的文件中 2 public 最高级别的访问权限 对访问公共成员没有限制 3 private 最低级别的访问权限 只能在声明它 ...
- 访问修饰符(public,private,protected,internal,sealed,abstract)
为了控件C#中的对象的访问权限,定义对象时可以在前面添加修饰符. 修饰符有五种:private(私有的),protected(受保护的),internal(程序集内部的),public(公开的),以及 ...
- 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)
访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...
- 对访问修饰关键字public, protected, internal and private的说明
对访问修饰关键字public, protected, internal and private的说明1.msdn: Internal types or members are accessible o ...
- protected internal修饰符
见过这样的修饰符,但是没有仔细考虑过,今天做一个小练习. 先给出一个链接,别人在网上讨论的:http://wenku.baidu.com/view/4023f65abe23482fb4da4cfe.h ...
- C#中public、private、protected、internal、protected internal (转载)
在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal.作用范围如下表:访问修饰符 说明public 公有访问.不受 ...
- C#访问修饰符(public,private,protected,internal,sealed,abstract)
为了控件C#中的对象的访问权限,定义对象时可以在前面添加修饰符. 修饰符有五种:private(私有的),protected(受保护的),internal(程序集内部的),public(公开的),以及 ...
随机推荐
- Shell输入/输出重定向
输出重定向 重定向一般通过在命令间插入特定的符号来实现.特别的,这些符号的语法如下所示 command1 >file1 上面这个命令执行command1然后将输出的内容存入file1. 注意任何 ...
- 梦想CAD控件安卓文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现代码如下: // 增加文字样式 //getCurrentDatabase()返回当前数据库对象 //getTextstyle() ...
- 梦想CAD控件安卓参数绘图
在CAD绘图中,参数化绘图可以帮助我们极大缩短设计时间,用户可以按照设计意图控制绘图对象,这样即使对象发生了变化,具体的关系和测量数据仍将保持不变,能够对几何图形和标注进行控制,可以帮助用户应对耗时的 ...
- JAVA基础——is-a 、have-a、和 like-a的区别
1.is-a,has-a,like-a是什么 在面向对象设计的领域里,有若干种设计思路,主要有如下三种: is-a.has-a.like-a java中在类.接口.抽象类中有很多体现. 了解java看 ...
- Chrome升级后打开新的标签页变样了……
最近更新Chrome后,打开新的标签页完全变样了,让人不知所措,特别是没有了那个“最近关闭标签页”按钮,这让我抓狂…… PS:Chrome版本号为:29.0.1547.76 m PPS:最新版已无法修 ...
- [Git]Please make sure you have the correct access rights and the repository exists
这个问题是这样,需要在已有github账号的A机器上,再创建一个github账号,新账号创建完毕,将代码通过机器A push上之后,再另一台机器B,clone 这个项目时报出了如下错误: Permis ...
- [LNOI2014]LCA(树链剖分)
BZOJ传送门 Luogu传送门 题目:给你一棵树,给你n个询问,每个询问要求输出$\sum_{i=l}^{r}depth(LCA(i,z))$ 细看看其实没有想象的那么难 大体思路: 1.对于每个询 ...
- fread快读+fwrite快速输出
定义数组 char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf; 读入 #define getchar() (p1==p2&a ...
- python3 判断大小写
转自http://wangwei007.blog.51cto.com/68019/1134323 # 一.pyhton字符串的大小写转换, 常用的有以下几种方法: # 1.对字符串中所有字符(仅对字母 ...
- lucas定理和组合数学
自湖南长沙培训以来的坑...一直未填,今天把这个问题解决掉. 参考: 1.http://www.cnblogs.com/Var123/p/5523068.html 2.http://blog.csdn ...