C# protected internal
示例
protectedinternal关键字标记字段,方法,属性和嵌套类为相同的组件内使用或派生类中另一组件:
组装1
public class Foo
{
public string MyPublicProperty { get; set; }
protected internal string MyProtectedInternalProperty { get; set; }
protected internal class MyProtectedInternalNestedClass
{
private string blah;
public int N { get; set; }
}
}
public class Bar
{
void MyMethod1()
{
Foo foo = new Foo();
var myPublicProperty = foo.MyPublicProperty;
var myProtectedInternalProperty = foo.MyProtectedInternalProperty;
var myProtectedInternalNestedInstance =
new Foo.MyProtectedInternalNestedClass();
}
}组装2
public class Baz : Foo
{
void MyMethod1()
{
var myPublicProperty = MyPublicProperty;
var myProtectedInternalProperty = MyProtectedInternalProperty;
var thing = new MyProtectedInternalNestedClass();
}
void MyMethod2()
{
Foo foo = new Foo();
var myPublicProperty = foo.MyPublicProperty;
// 编译错误
var myProtectedInternalProperty = foo.MyProtectedInternalProperty;
// 编译错误
var myProtectedInternalNestedInstance =
new Foo.MyProtectedInternalNestedClass();
}
}
public class Qux
{
void MyMethod1()
{
Baz baz = new Baz();
var myPublicProperty = baz.MyPublicProperty;
// 编译错误
var myProtectedInternalProperty = baz.MyProtectedInternalProperty;
// 编译错误
var myProtectedInternalNestedInstance =
new Baz.MyProtectedInternalNestedClass();
}
void MyMethod2()
{
Foo foo = new Foo();
var myPublicProperty = foo.MyPublicProperty;
//编译错误
var myProtectedInternalProperty = foo.MyProtectedInternalProperty;
// 编译错误
var myProtectedInternalNestedInstance =
new Foo.MyProtectedInternalNestedClass();
}
}
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短