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#的更多相关文章

  1. c#中的访问修饰符Protected,privet ,public, internal,和internal protected

    Protected,privet ,public, internal,和internal protected的区别 Private修饰的,只能值类内部使用,外部不可以使用,子类不能直接访问,但可以通过 ...

  2. 简述private,protected,public,internal修饰符的访问权限

    private:私有成员,在类的内部才可以访问 protected:保护成员,在类的内部和继承类中可以访问 public:公共成员,完全公开,没有访问限制 internal:当前程序集内可以访问

  3. c# protected public private internal

    1 internal 只能在一个项目中引用,不能跨项目引用,只有在同一程序集的文件中 2 public 最高级别的访问权限 对访问公共成员没有限制 3 private 最低级别的访问权限 只能在声明它 ...

  4. 访问修饰符(public,private,protected,internal,sealed,abstract)

    为了控件C#中的对象的访问权限,定义对象时可以在前面添加修饰符. 修饰符有五种:private(私有的),protected(受保护的),internal(程序集内部的),public(公开的),以及 ...

  5. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

    访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...

  6. 对访问修饰关键字public, protected, internal and private的说明

    对访问修饰关键字public, protected, internal and private的说明1.msdn: Internal types or members are accessible o ...

  7. protected internal修饰符

    见过这样的修饰符,但是没有仔细考虑过,今天做一个小练习. 先给出一个链接,别人在网上讨论的:http://wenku.baidu.com/view/4023f65abe23482fb4da4cfe.h ...

  8. C#中public、private、protected、internal、protected internal (转载)

    在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal.作用范围如下表:访问修饰符 说明public 公有访问.不受 ...

  9. C#访问修饰符(public,private,protected,internal,sealed,abstract)

    为了控件C#中的对象的访问权限,定义对象时可以在前面添加修饰符. 修饰符有五种:private(私有的),protected(受保护的),internal(程序集内部的),public(公开的),以及 ...

随机推荐

  1. vba txt读写的几种方式

    四种方式写txt 1.这种写出来的是ANSI格式的txt Dim TextExportFile As String TextExportFile = ThisWorkbook.Path & & ...

  2. Microsoft SQL Server 存储过程

    Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...

  3. A3. JVM 类加载器

    [概述] 虚拟机设计团队把类加载阶段中的 “通过一个类的全限定名来获取描述此类的二进制字节流” 这个动作放到 Java 虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码 ...

  4. enote笔记语言(3)(ver0.4)

    章节:enote笔记语言(3)     what&why(why not)&how&when&where&which:紫色,象征着神秘而又潜蕴着强大的力量,故取 ...

  5. TestNG套件测试(二)

    在xml中指定要运行的整个包来执行套件测试 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  6. FileOutputStream将从一个文件中读取的内容写到另一个文件中

    package com.janson.day2018082 import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  7. Python使用Flask框架,结合Highchart,搭配数据功能模块,加载 HTML 表格数据

    参考链接:https://www.highcharts.com.cn/docs/data-modules 1.javascript代码 var chart = Highcharts.chart('co ...

  8. 类与类之间的关系UML模型图

    关联.依赖.聚合.组合.泛化.实现 类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition ...

  9. linux命令整理版(拷贝)

    一.文件和目录操作命令 1.pwd 显示当前所在位置 -L 显示逻辑路径,忽略软链接文件 -P 显示物理路径时如果当前目录路径时软链接文件,则会显示软链接对应的源文件 2.cd 切换目录 cd - 回 ...

  10. 第一章 Linux命令行简介

    1 Linux系统命令操作语法的格式 命令_[参数选项]_[文件或路径]    其中 _ 至少一个空格    如:rm -f /etc/hosts    其中/etc/hosts完整路径不带空格   ...