C# Access Modifiers/Specifiers3 min read

Access modifiers or specifiers in C# define the accessibility of the member, class, or datatype in a program. These are the keyword that restricts unwanted manipulation of data in a program by other classes.

There are five types of access specifiers in C#.

  1. Public
  2. Protected
  3. Internal
  4. Protected internal
  5. Private

Let us go through each of them with an example.

Public Access Specifier

Declaring fields and methods or classes public means making it accessible everywhere in a program or in an application. It is accessed by the entire program. Any public member can be accessed from outside the class.

Example: C# program to demonstrate public access specifier.

Output:

Name: John Mac
Age: 15
My name is: John Mac


Protected Access Specifier

Declaring fields and methods protected means limiting its accessibility only to its class and derived types of this class. In the case of inheritance, the sub-class can access the private member of the derived class.

Example: C# program to demonstrate protected access specifier.

Output:

Animals eat.
Barking…


Internal Access Specifier

Declaring fields, methods, or class internal means limiting its accessibility only within files in the current assembly. It is a default specifier in the C# program, internal members can be accessed anywhere within its namespace.

Example: C# program to demonstrate internal access specifier.

Output:

My name is: Jason Marsh
Age: 16


Protected Internal Access Specifier

Declaring fields or methods protected internal means limiting its accessibility to the current assembly and within a derived class in another assembly.

Example: C# program to demonstrate protected internal access specifier.

Output:

My name is: Jason Marsh
Age: 16


Private Access Specifier

It is specified using the private keyword. Variables, Methods, and constructors that are declared private modifiers are only accessed within the declared class itself. It is the most access restricting specifier among all. This specifier is mostly used when you want to hide your members.

It is used to achieve encapsulation in C#. If you want to access the data, we use the set and get method.

Example: C# program to demonstrate private access specifier.

Output:

Student Name: John Mac
Studen Roll: 15


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More