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(公开的),以及 ...
随机推荐
- CSS:div/img水平垂直居中
div水平垂直居中方法一: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- css--小白入门篇2
一.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. 1 <style type ...
- 2n皇后 - 回溯
题目地址:http://www.51cpc.com/web/problem.php?id=1172 Summarize: 1. 递归回溯: 2. 先扫完一种皇后,再扫描另一种: 3. 循环输入: 4. ...
- Luogu P3797 妖梦斩木棒
解题思路 用线段树做这个就不用说了吧,但是要维护的东西确实很神奇.在每一个节点上都维护一个$lbkt$,表示这个区间上最靠左的右括号的位置:一个$rbkt$,表示这个区间上最靠右的左括号的位置.还有一 ...
- UVA - 514 Rails(栈模拟)
题目: 给出一个序列,问将1,2,3,4……按从小到大的顺序入栈,能否得到给出的序列. 思路: 用stack模拟就可以了. 当前的cnt如果小于a[i],就将cnt入栈,否则就判断栈顶是不是和a[i] ...
- Linux学习笔记记录(九)
- 可以通过dict[key]获得dict[value]
dict={key:value,key2:value2} print (dict[key] ) 得到的是 dict[value] # 软文预存接口,通过key来预览未保存的软文,联查商品.kol ...
- js之字典操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Unity对象的所有组件深拷贝与粘贴
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51454847 作者:car ...
- 一些非常有用的工具类之javamail(from韩顺平)
之前编写一个类淘宝服务器时,需要使用javamail发送邮件,搜到的一个工具类,很有用. 需要下载导入:activation.jar和mail.jar package com.cx.service; ...