Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explains how protected internal works with the virtual and override keywords in different assemblies. #39192

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/csharp/language-reference/keywords/protected-internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,47 @@ In the second file, an attempt to access `myValue` through an instance of `BaseC

Struct members cannot be `protected internal` because the struct cannot be inherited.

## Overriding protected internal members

When overriding a virtual member, the accessibility modifier of the overriden method will depend on if it defined in the same assembly as the class it is deriving from.
BillWagner marked this conversation as resolved.
Show resolved Hide resolved
If the derived class is defined in the same assembly that the base class is defined in, all overriden members will have the accessibility modifier `protected internal`. If the derived class is defined in a different assembly from where the base class is defined, overriden members will only have the `protected` accessibility modifier.
BillWagner marked this conversation as resolved.
Show resolved Hide resolved

```csharp
// Assembly1.cs
// Compile with: /target:library
public class BaseClass
{
protected internal virtual int GetExampleValue()
{
return 5;
}
}

public class DerivedClassSameAssembly : BaseClass
{
// Override to return a different example value, accessibility modifiers remain the same.
protected internal override int GetExampleValue()
{
return 9;
}
}
```

```csharp
// Assembly2.cs
// Compile with: /reference:Assembly1.dll
class DerivedClassDifferentAssembly : BaseClass
{
// Override to return a different example value, since this override
// method is defined in another assembly, the accessibility modifiers
// are only protected, instead of protected internal.
protected override int GetExampleValue()
{
return 2;
}
}
```

## C# language specification

[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
Expand Down
Loading