Home  >  Article  >  Backend Development  >  What is the scope of protected member variables of a class in C#?

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

WBOY
WBOYforward
2023-09-10 18:17:08855browse

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 -

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete