When you're building an application, you can think of your code like a house. Some rooms are open to guests (the public), while others are private (like a safe or a bedroom). In C#, Access Modifiers are the "locks and keys" that define who can see or use different parts of your code.

Without them, your code would be messy and insecure -anyone could change anything at any time.


The Big Four (and Two Extras)

C# provides six levels of access, but you'll use the first three about 90% of the time.

1. Public (public)

The "Open Door" policy. If you mark a class or a variable as public, it can be accessed from anywhere—inside the project or even from another project that uses your code.

  • Best for: Methods or properties that other parts of the app need to interact with.

2. Private (private)

The "Secret Vault." This is the default for class members. If you don't specify an access modifier, it's usually private. It can only be accessed within the same class or struct.

  • Best for: Internal logic or sensitive data that shouldn't be touched by outside code.

3. Protected (protected)

The "Family Heirloom." This is slightly more open than private. It can be accessed by the class it’s in and by any class that inherits from it (child classes).

  • Best for: Base classes where you want "children" to share certain traits but keep them hidden from the rest of the world.

4. Internal (internal)

The "Office Memo." This is visible to everything inside the same project (assembly), but invisible to anyone outside that project.

  • Best for: Tools or classes that help your project run but aren't meant for external users.

5. Protected Internal

A mix of Protected and Internal. Accessible within the same project or by a child class in a different project.

6. Private Protected

The strictest combo. Accessible only by the class it’s in or child classes within the same project.

A Real-World Example: The ATM

Imagine you are coding a simple ATM system. Here is how you would apply these modifiers:

public class BankAccount
{
    // Anyone can see the account number
    public string AccountNumber = "123456789";

    // Only the BankAccount class can see or change the PIN
    private int _pinCode = 1122;

    // Only this class and 'SavingsAccount' (child) can see the interest rate
    protected double InterestRate = 0.05;

    public void CheckBalance()
    {
        // This method is public so the user can call it
        Console.WriteLine("Your balance is safe.");
    }
}


Why Should You Care?

Using access modifiers isn't just about being organized; it’s about Encapsulation. By hiding the "inner workings" of your code (making them private) and only showing what’s necessary (making them public), you make your code:

  1. Easier to maintain: You can change the private logic without breaking other parts of the app.

  2. More secure: You prevent accidental changes to critical data.

  3. Cleaner: Other developers using your code only see what they actually need to use.

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions