What is the scope of a protected member variable of a class in C#?


Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.

The following is an example showing we have set an protected member variable in Class A.

class A {
   protected int a2 = 87;
}

Now under the derived class when we will try to access the above variable from the derived class object, then it would work fine as shown below −

Example

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;
   }
}

Updated on: 23-Jun-2020

313 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements