Introduction
Access modifiers in C# are keywords used to specify the accessibility or visibility of classes, methods, properties, fields, and other members. They control the scope of accessibility of these members within the code. Understanding access modifiers is essential for encapsulation in object-oriented programming.
Types of Access Modifiers
- public
- private
- protected
- internal
- protected internal
- private protected
1. public
The public
access modifier makes the member accessible from any other code in the same assembly or another assembly that references it. It is the most permissive access level.
Example
using System;
namespace AccessModifiersExample
{
public class PublicExample
{
public string PublicField = "Public Field";
public void Display()
{
Console.WriteLine(PublicField);
}
}
class Program
{
static void Main(string[] args)
{
PublicExample example = new PublicExample();
example.Display(); // Accessible
Console.WriteLine(example.PublicField); // Accessible
}
}
}
Output
Public Field
Public Field
2. private
The private
access modifier makes the member accessible only within the body of the class or struct in which it is declared. It is the least permissive access level.
Example
using System;
namespace AccessModifiersExample
{
public class PrivateExample
{
private string PrivateField = "Private Field";
public void Display()
{
Console.WriteLine(PrivateField);
}
}
class Program
{
static void Main(string[] args)
{
PrivateExample example = new PrivateExample();
example.Display(); // Accessible
// Console.WriteLine(example.PrivateField); // Not accessible
}
}
}
Output
Private Field
3. protected
The protected
access modifier makes the member accessible within its own class and by derived class instances.
Example
using System;
namespace AccessModifiersExample
{
public class ProtectedExample
{
protected string ProtectedField = "Protected Field";
public void Display()
{
Console.WriteLine(ProtectedField);
}
}
public class DerivedExample : ProtectedExample
{
public void Show()
{
Console.WriteLine(ProtectedField); // Accessible in derived class
}
}
class Program
{
static void Main(string[] args)
{
DerivedExample example = new DerivedExample();
example.Show(); // Accessible
// Console.WriteLine(example.ProtectedField); // Not accessible
}
}
}
Output
Protected Field
Protected Field
4. internal
The internal
access modifier makes the member accessible only within files in the same assembly.
Example
using System;
namespace AccessModifiersExample
{
internal class InternalExample
{
internal string InternalField = "Internal Field";
internal void Display()
{
Console.WriteLine(InternalField);
}
}
class Program
{
static void Main(string[] args)
{
InternalExample example = new InternalExample();
example.Display(); // Accessible
Console.WriteLine(example.InternalField); // Accessible
}
}
}
Output
Internal Field
Internal Field
5. protected internal
The protected internal
access modifier makes the member accessible within its own assembly and also by derived class instances in other assemblies.
Example
using System;
namespace AccessModifiersExample
{
public class ProtectedInternalExample
{
protected internal string ProtectedInternalField = "Protected Internal Field";
public void Display()
{
Console.WriteLine(ProtectedInternalField);
}
}
public class DerivedExample : ProtectedInternalExample
{
public void Show()
{
Console.WriteLine(ProtectedInternalField); // Accessible in derived class
}
}
class Program
{
static void Main(string[] args)
{
DerivedExample example = new DerivedExample();
example.Show(); // Accessible
Console.WriteLine(example.ProtectedInternalField); // Accessible within the same assembly
}
}
}
Output
Protected Internal Field
Protected Internal Field
6. private protected
The private protected
access modifier makes the member accessible within its containing class or derived classes, but only within the same assembly.
Example
using System;
namespace AccessModifiersExample
{
public class PrivateProtectedExample
{
private protected string PrivateProtectedField = "Private Protected Field";
public void Display()
{
Console.WriteLine(PrivateProtectedField);
}
}
public class DerivedExample : PrivateProtectedExample
{
public void Show()
{
Console.WriteLine(PrivateProtectedField); // Accessible in derived class within the same assembly
}
}
class Program
{
static void Main(string[] args)
{
DerivedExample example = new DerivedExample();
example.Show(); // Accessible
// Console.WriteLine(example.PrivateProtectedField); // Not accessible outside the class or derived class
}
}
}
Output
Private Protected Field
Private Protected Field
Conclusion
Access modifiers in C# are essential for controlling the visibility and accessibility of class members. By understanding and using public
, private
, protected
, internal
, protected internal
, and private protected
, you can design your classes to encapsulate data properly and expose only what is necessary. This helps in building secure and maintainable applications.