Home > Backend Development > C#.Net Tutorial > 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
Release: 2023-09-10 18:17:08
forward
922 people have browsed it

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;
}
Copy after login

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;
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template