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

CsharpProgrammingServer Side Programming

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;
   }
}
raja
Published on 18-Sep-2018 08:14:46
Advertisements