Home >Backend Development >C#.Net Tutorial >What is the scope of protected member variables of a class in C#?
Protected access specifiers allow subclasses to access member variables and member functions of their base class. This helps with inheritance. We will discuss this in more detail in the inheritance chapter.
The following is an example showing that we set a protected member variable in class A.
class A { protected int a2 = 87; }
Now under the derived class, when we try to access the above variable from the derived class object, it will work fine as shown below -
using System; class A { protected int a2 = 87; } class B : A { static void Main() { A a = new A(); B b = new B(); b.a2 = 10; } }
The above is the detailed content of What is the scope of protected member variables of a class in C#?. For more information, please follow other related articles on the PHP Chinese website!