Access modifiers in C# are keywords used to specify the accessibility or visibility of types
and type members [fields, methods, properties, etc.] within a
program.
They control how classes, structs, interfaces, and their members
can be accessed from other parts of the program.
There are five access modifiers in C#
public: The member is accessible from any other
code in the same assembly or referencing assembly.
private: The member is accessible only within the
containing type (class or struct).
protected: The member is accessible within the
containing type and by derived types.
internal: The member is accessible only within the
same assembly.
protected internal: The member is accessible within the same assembly and by derived types.
using System;
// Base class with different access modifiers
public class MyClass
{
public int PublicField; //
Public field
private int PrivateField; //
Private field
protected int ProtectedField; // Protected field
internal int InternalField; //
Internal field
protected internal int ProtectedInternalField; // Protected internal field
// Constructor
public MyClass()
{
PublicField = 10;
PrivateField = 20;
ProtectedField = 30;
InternalField = 40;
ProtectedInternalField = 50;
}
// Method with public access modifier
public void PublicMethod()
{
Console.WriteLine("Public method");
}
// Method with private access modifier
private void PrivateMethod()
{
Console.WriteLine("Private method");
}
// Method with protected access modifier
protected void ProtectedMethod()
{
Console.WriteLine("Protected method");
}
// Method with internal access modifier
internal void InternalMethod()
{
Console.WriteLine("Internal method");
}
// Method with protected internal access modifier
protected internal void ProtectedInternalMethod()
{
Console.WriteLine("Protected internal method");
}
}
// Derived class
public class DerivedClass : MyClass
{
public void AccessProtectedField()
{
// Accessing protected field from the base class
Console.WriteLine($"Accessing protected field from derived class: {ProtectedField}");
}
}
class Program
{
static void Main(string[] args)
{
// Creating an object of MyClass
MyClass myObj = new MyClass();
// Accessing members of MyClass
Console.WriteLine($"Public field: {myObj.PublicField}");
// Console.WriteLine($"Private field: {myObj.PrivateField}");
// Error: PrivateField is inaccessible
// Console.WriteLine($"Protected field:
{myObj.ProtectedField}"); // Error: ProtectedField is inaccessible
Console.WriteLine($"Internal field: {myObj.InternalField}");
Console.WriteLine($"Protected internal field: {myObj.ProtectedInternalField}");
// Calling methods of MyClass
myObj.PublicMethod();
// myObj.PrivateMethod(); // Error: PrivateMethod is inaccessible
// myObj.ProtectedMethod(); // Error: ProtectedMethod is
inaccessible
myObj.InternalMethod();
myObj.ProtectedInternalMethod();
// Creating an object of DerivedClass
DerivedClass
derivedObj = new DerivedClass();
derivedObj.AccessProtectedField();
// Accessing protected field from the derived
class
}
}