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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

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 …