protected internal C# with Example



protected internal C# with Example

The protected internal keyword marks field, methods, properties and nested classes for use inside the same 
assembly or derived classes in another assembly: 
Assembly 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(); 
} 
} 
Assembly 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; 
// Compile Error 
var myProtectedInternalProperty = foo.MyProtectedInternalProperty; 
// Compile Error 
var myProtectedInternalNestedInstance = 
new Foo.MyProtectedInternalNestedClass(); 
} 
} 
public class Qux 
{ 
void MyMethod1() 
{ 
Baz baz = new Baz(); 
var myPublicProperty = baz.MyPublicProperty; 
// Compile Error 
var myProtectedInternalProperty = baz.MyProtectedInternalProperty; 
// Compile Error 
var myProtectedInternalNestedInstance = 
new Baz.MyProtectedInternalNestedClass(); 
} 
void MyMethod2() 
{ 
Foo foo = new Foo(); 
var myPublicProperty = foo.MyPublicProperty; 
//Compile Error 
var myProtectedInternalProperty = foo.MyProtectedInternalProperty; 
// Compile Error 
var myProtectedInternalNestedInstance = 
new Foo.MyProtectedInternalNestedClass(); 
} 
} 

0 Comment's

Comment Form