0
0
CsharpConceptBeginner · 4 min read

Access Modifiers in C#: What They Are and How to Use Them

In C#, access modifiers are keywords that control the visibility of classes, methods, and variables to other parts of the program. They help protect data and organize code by limiting where members can be accessed, such as public, private, protected, and internal.
⚙️

How It Works

Think of access modifiers like doors with different locks in a house. Some doors are open to everyone, some only to family members, and some only to the owner. In C#, access modifiers decide who can enter or use parts of your code, like classes or methods.

For example, public means anyone can use it, like an open door. private means only the code inside the same class can use it, like a locked door only you have the key to. This helps keep your code safe and organized by hiding details that other parts don’t need to see.

💻

Example

This example shows different access modifiers in action inside a class:

csharp
using System;

class Person
{
    public string Name;          // Anyone can see and change
    private int age;             // Only this class can see
    protected string Secret;     // This class and subclasses can see
    internal string City;        // Only code in the same assembly can see

    public void SetAge(int newAge)
    {
        if (newAge > 0) age = newAge;  // Control access to age
    }

    public void ShowInfo()
    {
        Console.WriteLine($"Name: {Name}, Age: {age}, City: {City}");
    }
}

class Employee : Person
{
    public void ShowSecret()
    {
        Secret = "I love coding";  // Allowed because of protected
        Console.WriteLine($"Secret: {Secret}");
    }
}

class Program
{
    static void Main()
    {
        Person p = new Person();
        p.Name = "Alice";
        p.SetAge(30);
        p.City = "New York";
        p.ShowInfo();

        Employee e = new Employee();
        e.Name = "Bob";
        e.SetAge(25);
        e.City = "Chicago";
        e.ShowInfo();
        e.ShowSecret();
    }
}
Output
Name: Alice, Age: 30, City: New York Name: Bob, Age: 25, City: Chicago Secret: I love coding
🎯

When to Use

Use access modifiers to protect important data and control how your code parts interact. For example, make variables private if you don’t want other parts of the program to change them directly. Use public for things you want everyone to use, like a method that shows information.

In real projects, this helps avoid mistakes like accidentally changing data or using parts of code in the wrong way. It also makes your code easier to understand and maintain by clearly showing what is meant to be used outside and what is kept hidden.

Key Points

  • Public: Accessible from anywhere.
  • Private: Accessible only within the same class.
  • Protected: Accessible within the class and its subclasses.
  • Internal: Accessible only within the same assembly.
  • Access modifiers help keep code safe, organized, and easier to maintain.

Key Takeaways

Access modifiers control who can see or use parts of your code in C#.
Use private to hide details and public to share functionality.
Protected allows access in subclasses, internal limits access to the project.
Proper use of access modifiers improves code safety and clarity.