Access Specifiers / Modifiers in C#

What are Access Specifiers in C#?

Access specifiers are keywords in C# that control how class members (variables, methods, etc.) can be accessed by other parts of the program. They ensure that only the necessary parts of the code are visible and accessible, promoting encapsulation and data security.

In C#, access specifiers control how class members can be accessed:

  • Public: Accessible from anywhere, broad visibility.
  • Private: Accessible only within the same class, hidden from external access.
  • Protected: Accessible within class and subclasses, hidden from the rest of the program.
  • Internal: Accessible within the same assembly, not from other assemblies.
  • Protected Internal: Accessible within assembly and subclasses.

The general syntax for access specifiers in C# is:

access_specifier member_type member_name;


  • access_specifier is one of the five access specifiers (public, private, protected, internal, protected internal).
  • member_type represents the data type or return type of the member.
  • member_name is the name of the class member.
Usage of Access Specifiers
  • Access specifiers in C# control the visibility and accessibility of class members.
  • They promote encapsulation and data security.
  • Access specifiers define what parts of the code can be accessed by other classes or code outside the current class.
  • Proper use of access specifiers ensures a clean and manageable interface for users and enhances code organization and security.
Access Specifiers Example:

Here is source code of the C# Program to Illustrate the Use of Access Specifiers.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
 * C# Program to Illustrate the Use of Access Specifiers
using System;
namespace accessspecifier
    class Program
        static void Main(string[] args)
            two B = new two();
    class one
        private int x;
        protected int y;
        internal int z;
        public int a;
        protected internal int b;
    class two : one
        public void show()
            Console.WriteLine("Values are : ");
            y = 20;
            z = 30;
            a = 40;
            b = 50;
            // Console.WriteLine(+x); // Error x is not accessible
Program Explanation

1. The program defines two classes, one and two, inside the accessspecifier namespace.
2. The one class has five member variables: x (private), y (protected), z (internal), a (public), and b (protected internal).
3. The two class inherits from the one class using the : syntax.
4. The show() method inside the two class displays the values of accessible variables (y, z, a, and b).
5. The x variable is commented out in the show() method as it is not accessible from the two class due to its private access specifier.

Program Output:
Values are :

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Check this: MCA MCQs | C# Books
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.