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

Access modifiers in C#

WBOY
WBOYforward
2023-09-15 08:37:02991browse

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

Output

Details: Website
Tabs: Product
Tabs: Services
Tabs: Tools
Tabs: Plugins

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