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;
Where:
- 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.
- 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.
Here is source code of the C# Program to Illustrate the Use of Access Specifiers.
/* * C# Program to Illustrate the Use of Access Specifiers */ using System; namespace accessspecifier { class Program { static void Main(string[] args) { two B = new two(); B.show(); } } 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 : "); //x=10; y = 20; z = 30; a = 40; b = 50; // Console.WriteLine(+x); // Error x is not accessible Console.WriteLine(y); Console.WriteLine(z); Console.WriteLine(a); Console.WriteLine(b); Console.ReadLine(); } } }
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.
Values are : 20 30 40 50
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Computer Science Books
- Buy MCA Books
- Apply for C# Internship
- Apply for Computer Science Internship
- Buy C# Books