Home > Backend Development > C#.Net Tutorial > Access modifiers in C#

Access modifiers in C#

WBOY
Release: 2023-09-15 08:37:02
forward
1003 people have browsed it

C# 中的访问修饰符

Access modifiers specify the scope of variables and functions in C#. The following are the access modifiers provided by C#:

Public

The public modifier places no restrictions on member access.

Protected

Access is limited to derived classes or class definitions. ​​

Internal

Internal access modifiers within a program with the following permissions access its declaration.

Protected Internal

It has access specifiers provided by both protected and internal access modifiers.

Private

Restricted to the class in which it is declared. Members designated as private cannot be accessed outside the class.

Example

Let us see an example of protected access modifier, accessing protected members -

Live Demonstration

using System;
namespace MySpecifiers {
   class Demo {
      protected string name = "Website";
      protected void Display(string str) {
         Console.WriteLine("Tabs: " + str);
      }
   }

   class Test : Demo {
      static void Main(string[] args) {
         Test t = new Test();
         Console.WriteLine("Details: " + t.name);
         t.Display("Product");
         t.Display("Services");
         t.Display("Tools");
         t.Display("Plugins");
      }
   }
}
Copy after login

Output

Details: Website
Tabs: Product
Tabs: Services
Tabs: Tools
Tabs: Plugins
Copy after login

The above is the detailed content of Access modifiers 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