Access Modifiers in C#


Access Modifiers specifies the scope of variable and functions in C#. The following are the access modifiers used provided by C#:

Public

The public modifier sets no restriction on the access of members.

Protected

Access limited to the derived class or class definition.

Internal

The Internal access modifier access within the program that has its declaration.

Protected internal

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

Private

Limited only inside the class in which it is declared. The members specified as private cannot be accessed outside the class.

Example

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

Live Demo

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

Updated on: 19-Jun-2020

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements