In object-oriented programming, encapsulation is a fundamental concept that involves bundling the data (attributes) and methods that operate on the data into a single unit or class. Encapsulation also restricts direct access to some of an object's components, which can prevent the accidental modification of data. Access modifiers in C# are keywords used to set the accessibility level of classes, methods, properties, fields, and other members.
Access modifiers determine how accessible a class member is from outside its own class. C# provides several access modifiers that control this visibility:
Let's explore each access modifier with practical examples.
public class Person
{
public string Name { get; set; }
public void Display()
{
Console.WriteLine("Name: " + Name);
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John Doe";
person.Display();
}
}
<OutputBlock>
{`Name: John Doe`}
In this example, the `Name` property and the `Display` method are public. Therefore, they can be accessed from outside the `Person` class.
### Private Access Modifier
```csharp
public class Person
{
private string name;
public void SetName(string newName)
{
name = newName;
}
public void Display()
{
Console.WriteLine("Name: " + name);
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
// person.name = "John Doe"; // This will cause a compile-time error
person.SetName("John Doe");
person.Display();
}
}
{`Name: John Doe`}
</OutputBlock>
Here, the `name` field is private. It cannot be accessed directly from outside the `Person` class. Instead, we use a public method `SetName` to modify it.
### Protected Access Modifier
```csharp
public class Person
{
protected string name;
public void SetName(string newName)
{
name = newName;
}
public void Display()
{
Console.WriteLine("Name: " + name);
}
}
public class Employee : Person
{
public void ShowName()
{
Console.WriteLine("Employee Name: " + name); // Accessible because it's a derived class
}
}
class Program
{
static void Main(string[] args)
{
Employee employee = new Employee();
employee.SetName("Jane Doe");
employee.Display();
employee.ShowName();
}
}
<OutputBlock>
{`Name: Jane Doe
Employee Name: Jane Doe`}
</OutputBlock>
In this example, the `name` field is protected. It can be accessed within the `Person` class and any derived classes like `Employee`.
### Internal Access Modifier
```csharp
internal class Person
{
public string Name { get; set; }
public void Display()
{
Console.WriteLine("Name: " + Name);
}
}
class Program
{
static void Main(string[] args)
{
// Person person = new Person(); // This will cause a compile-time error if the classes are in different assemblies
// person.Name = "John Doe";
// person.Display();
}
}
In this example, the Person class is internal. It can only be accessed within the same assembly.
protected internal class Person
{
public string Name { get; set; }
public void Display()
{
Console.WriteLine("Name: " + Name);
}
}
public class Employee : Person
{
public void ShowName()
{
Console.WriteLine("Employee Name: " + Name); // Accessible because it's a derived class in another assembly
}
}
class Program
{
static void Main(string[] args)
{
Employee employee = new Employee();
employee.Name = "Jane Doe";
employee.Display();
employee.ShowName();
}
}
Name: Jane Doe Employee Name: Jane Doe
Here, the Person class is protected internal. It can be accessed within the same assembly or from derived classes in other assemblies.
After mastering access modifiers, you should explore how to use them effectively with properties in C#. Properties provide a way to encapsulate fields and control their access through get and set methods. Understanding properties will further enhance your ability to implement robust and secure object-oriented designs.
Stay tuned for more tutorials on advanced C# concepts!