Object-Oriented Programming came up with some new concepts and characteristics. Among them, securing the data through different means is also a crucial method. Encapsulating means wrapping up data into a particular unit so that accessing these data needs special ways and principles. That is where the concept of access modifiers or access specifiers comes. In this chapter, you will learn about the different access modifiers and their use in various domains.
What Are Access Modifiers In C#?
Access modifiers are also known as access specifiers used to preserve the concept of encapsulation of data by specifically restricting the access of variable data using some particular keywords. These are used to specify a specific variable's accessibility or scope and functionality within a C# application.
- Public.
- Protected.
- Internal.
- Protected internal.
- Private.
Let us discuss each of them in details:
- Public Access Modifier is one of the modifiers that allow programmers to expose all of its member variables and methods outside the class scope. Members of a class that are declared as public can be accessed from anywhere within the program.
- Protected Access Modifier: is associated with a property or a method where class members can be accessed only by classes (derived classes) inherited from the present class (base class).
- Internal Access Modifier: is used to restrict the access entirely to classes distinct within the existing project. In other words, members of a class with internal access specifier can be accessed from other classes or methods specified within the project application.
- Protected Internal Access Modifier: allows any class in hiding the member variables and class methods from additional and existing class objects and their associated methods, apart from the child class(es) that are there within the same program or application.
- Private Access Modifier: is another access specifier that allows programmers to hide or preserve the member variables and methods from other member functions or class objects. The private members of a class can be accessed by only the functions belonging to the same class.
C# Program to Demonstrate Public, Private and Protected Access Modifiers
Example:
using System;
namespace AccessModifiers
{
class Testing
{
private int pi = 3;
public string name = "Richerd Jones";
public void PRINT(string msg)
{
Console.WriteLine("Hi " + msg);
}
protected void PRINTED(string msg)
{
Console.WriteLine("Welcome " + msg);
}
}
class Test : Testing
{
static void Main(string[] args)
{
Testing am = new Testing();
Test t = new Test();
Console.WriteLine(" Hi " + am.name);
am.PRINT("Richerd");
t.PRINTED("Mr. Richerd");
}
}
}
Output:
Hi Richerd Jones Hi Richerd Welcome Mr. Richerd
C# Program to Demonstrate Protected Internal Access Modifier
Example:
using System;
namespace AccessSpecifiers
{
class TestClass
{
protected internal string name = "Richerd Jones";
protected internal void Print(string msg)
{
Console.WriteLine("Hey " + msg);
}
}
class Test2
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
Console.WriteLine(" Welcome " + tc.name);
tc.Print("Richerd Jones");
}
}
}
Output:
Welcome Richerd Jones Hey Richerd Jones