protected internal (C# Reference)
The protected internal
keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class. For a comparison of protected internal
with the other access modifiers, see Accessibility Levels.
Example
A protected internal member of a base class is accessible from any type within its containing assembly. It is also accessible in a derived class located in another assembly only if the access occurs through a variable of the derived class type. For example, consider the following code segment:
// Assembly1.cs
// Compile with: /target:library
public class BaseClass
{
protected internal int myValue = 0;
}
class TestAccess
{
void Access()
{
var baseObject = new BaseClass();
baseObject.myValue = 5;
}
}
// Assembly2.cs
// Compile with: /reference:Assembly1.dll
class DerivedClass : BaseClass
{
static void Main()
{
var baseObject = new BaseClass();
var derivedObject = new DerivedClass();
// Error CS1540, because myValue can only be accessed by
// classes derived from BaseClass.
// baseObject.myValue = 10;
// OK, because this class derives from BaseClass.
derivedObject.myValue = 10;
}
}
This example contains two files, Assembly1.cs
and Assembly2.cs
.
The first file contains a public base class, BaseClass
, and another class, TestAccess
. BaseClass
owns a protected internal member, myValue
, which is accessed by the TestAccess
type.
In the second file, an attempt to access myValue
through an instance of BaseClass
will produce an error, while an access to this member through an instance of a derived class, DerivedClass
will succeed.
Struct members cannot be protected internal
because the struct cannot be inherited.
C# language specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
See also
.NET feedback
The .NET documentation is open source. Provide feedback here.
Feedback
Submit and view feedback for