Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

What is the private protected access modifier in C#?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The private protected access modifier is introduced in C# 7.0 and specifies the following rules:

  • Only the derived class in the same assemble can access the private protected members.
  • Base class in the same or different assembly cannot access the private protected members.
  • The private protected members of an assembly cannot be accessed by a derived class of another assembly. However, if, in the former assembly, InternalsVisibleToAttribute contains the name of the latter assembly, then the private protected members can be accessed in the latter assembly.

Examples

The following code demonstrates the usage of the private protected access modifier:

class Base {
private protected x;
}
class Derived_1 : Base {
}
class Program {
static void Main() {
Derived_1 d1 = new Derived_1();
d1.x = 10;
Base b = new Base()
b.x = 100; // Error on this line
}
}

In the example above, the Base class has a private protected member x. The Derived_1 class inherits the Base class. In the Main method, d1 is initialized with a Derived_1 instance and b is initialized with a Base instance. Line 11 won’t give an error; however, line 14 will give an error because a base class is trying to access its own private protected member.

The following code provides an example that contains two assembly files:

assembly1.cs
assembly2.cs
class Derived_2 : Base {
}
class Program {
static void Main() {
Derived_1 d1 = new Derived_1();
d1.x = 10;
Derived_2 d2 = new Derived_2();
d2.x = 10; // Error on this line
}
}

In the example above, assembly2.cs uses the assembly1.cs classes. In assembly1.cs, the Base and Derived_1 classes are defined as in the previous example. In the assembly2.cs file, the Derived_2 class is defined to inherit the Base class.

In the Main method, d1 stores an instance of the Derived_1 class, and d2 stores an instance of the Derived_2 class.

Line 7 doesn’t give an error, but line 10 does give an error because a derived class in another assembly file is trying to access a private protected member of a different assembly file.

RELATED TAGS

c#

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring