Skip to content

Commit

Permalink
Explains how protected internal works with the virtual and override k…
Browse files Browse the repository at this point in the history
…eywords in different assemblies. (#39192)

* Update protected-internal.md

* Updates the sub-header

* Makes the comment line length shorter

* Apply suggestions from code review

---------

Co-authored-by: Bill Wagner <[email protected]>
  • Loading branch information
PowerfulBacon and BillWagner authored Jan 19, 2024
1 parent 271cf79 commit 91188a1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 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,48 @@ 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 overridden method depends on the assembly where the derived class is defined.

When the derived class is defined in the same assembly as the base class, all overridden members have `protected internal` access. If the derived class is defined in a different assembly from the base class, overridden members have `protected` access.

```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

0 comments on commit 91188a1

Please sign in to comment.