Img
Deepak AsatiSoftware Developerauthor linkedin
Published On
Updated On
Table of Content
up_arrow

What Are Access Modifiers in C#?

In C#, access modifiers are special keywords used to define who can see and use different parts of your code. They play a crucial role in organizing, protecting, and maintaining your code. By using these access modifiers, you control where and how the members (like methods and variables) of a class can be accessed.

In this blog, we'll explain the different access modifiers in C#, their purpose, and how they help in organizing your code. We'll also give you easy-to-understand examples along the way.

Why Do We Need Access Modifiers?

Access modifiers play a critical role in structuring and managing code in object-oriented programming (OOP) by defining the visibility and accessibility of classes, methods, variables, and other members. Here’s a deeper look into why they are essential:

Organize Your Code

Access modifiers provide a clear indication of which parts of the code are meant for external use and which are meant for internal purposes. This helps programmers working on a large project know exactly what they should and shouldn’t use, avoiding accidental modification of sensitive areas.

  • Example: A class might have a public method that other parts of the code can interact with, while the logic behind the scenes is kept in private methods. This separation enhances clarity.

Protect Your Code

Some parts of the code are meant to be hidden to protect against unauthorized access or modifications. Using access modifiers like private ensures that only specific parts of the code have access to critical or sensitive data. This is crucial for maintaining the integrity of the code and for security purposes.

  • Example: Sensitive calculations or data transformations are kept private so that only the class itself can modify them.

Encapsulate

Encapsulation is a fundamental OOP concept that involves bundling the data and the methods that operate on that data into a single unit or class. Access modifiers enforce encapsulation by ensuring that only the necessary parts of the class are exposed to the outside world.

  • Example: You can expose a method like GetData() as public, while the data itself is stored in a private field, making it inaccessible directly from outside the class.

Improve Maintainability

Access modifiers help improve the maintainability of code by making it easier for other programmers (or even your future self) to understand which parts of the codebase are safe to interact with. If an area is marked as private, it's clear that changing it may break internal logic, whereas public areas are safe to use or extend.

Types of Access Modifiers in C#

C# provides several access modifiers to define the visibility of classes and members. Each serves a different purpose, allowing you to customize the accessibility of code elements as needed. Below are the most commonly used access modifiers in C#:

C# offers five main access modifiers:

  • public
  • private
  • protected
  • internal
  • file

Additionally, you can combine some of them to form more specific access levels like protected internal and private protected.

Public Access Modifier

  • Definition: Members marked as public are accessible from anywhere, both inside the same class and from outside the class, even in other assemblies (projects).
  • Use Case: When you want something to be accessible from everywhere in your code.
public class MyClass
{
public int PublicField;

public void PublicMethod()
{
PublicField = 20;
}
}

MyClass obj = new MyClass();
obj.PublicField = 30; // Accessible from anywhere
obj.PublicMethod(); // Accessible from anywhere


In this example, PublicField and PublicMethod can be accessed from anywhere in the program.

Private Access Modifier

  • Definition: Members marked as private can only be accessed within the same class.
  • Use Case: When you want to hide sensitive data or methods from being accessed outside the class.
public class MyClass
{
private int PrivateField;

private void PrivateMethod()
{
PrivateField = 10;
}
}

MyClass obj = new MyClass();
// obj.PrivateField = 20; // Error: Cannot access private member
// obj.PrivateMethod(); // Error: Cannot access private method


Here, PrivateField and PrivateMethod are hidden and can only be accessed inside the MyClass. Other parts of the code cannot directly modify or access them.

Protected Access Modifier

  • Definition: Members marked as protected can be accessed within the same class and in any class that inherits from it.
  • Use Case: When you want to allow derived classes to access certain members, but not from outside the inheritance chain.
public class MyClass
{
protected int ProtectedField;

protected void ProtectedMethod()
{
ProtectedField = 30;
}
}

public class DerivedClass : MyClass
{
public void AccessProtectedMember()
{
ProtectedField = 40; // Accessible in derived class
ProtectedMethod(); // Accessible in derived class
}
}

DerivedClass derivedObj = new DerivedClass();
derivedObj.AccessProtectedMember(); // Works fine
// derivedObj.ProtectedField = 50; // Error: Cannot access from outside


In this case, ProtectedField and ProtectedMethod are available to any class that inherits from MyClass, but not outside of it.

Internal Access Modifier

  • Definition: Internal members can be accessed from anywhere within the same assembly (project), but not from other assemblies.
  • Use Case: When you want to share code only within a single project but not outside of it.
public class MyClass
{
internal int InternalField;

internal void InternalMethod()
{
InternalField = 50;
}
}

MyClass obj = new MyClass();
obj.InternalField = 60; // Accessible within the same assembly
obj.InternalMethod(); // Accessible within the same assembly


Here, InternalField and InternalMethod are accessible as long as you're in the same project, but not from another project.

Protected Internal Access Modifier

  • Definition: Protected internal members can be accessed within the same assembly or by derived classes in another assembly.
  • Use Case: When you want to share code within the same project but also allow derived classes from other projects to access it.

public class MyClass
{
protected internal int ProtectedInternalField;

protected internal void ProtectedInternalMethod()
{
ProtectedInternalField = 60;
}
}

MyClass obj = new MyClass();
obj.ProtectedInternalField = 70; // Accessible within the same assembly


In this example, ProtectedInternalField is accessible either within the same project or by any class that inherits from MyClass, even in another project.

Private Protected Access Modifier

  • Definition: Private protected members are accessible within the same class and by derived classes, but only if the derived class is in the same assembly.
  • Use Case: To allow inheritance within the same project but restrict external access.
public class BaseClass
{
private protected int PrivateProtectedField;

private protected void PrivateProtectedMethod()
{
Console.WriteLine("Private Protected Method");
}
}

public class DerivedClass : BaseClass
{
public void AccessPrivateProtectedMember()
{
PrivateProtectedField = 80; // Accessible in derived class
PrivateProtectedMethod(); // Accessible in derived class
}
}


Here, PrivateProtectedField and PrivateProtectedMethod can only be accessed within BaseClass or DerivedClass, but only if they are in the same project.

File Access Modifier

  • Definition: The file access modifier restricts the visibility of a type to the file in which it is declared.
  • Use Case: To hide the implementation details of types within a single file.
file class FilePrivateClass
{
public void FilePrivateMethod()
{
Console.WriteLine("This method is in a file-private class.");
}
}

public class PublicClass
{
public void PublicMethod()
{
FilePrivateClass filePrivate = new FilePrivateClass();
filePrivate.FilePrivateMethod(); // Accessible within the same file
}
}


In this example, FilePrivateClass and its members are hidden from other files, even in the same project.

Default Access Levels

If you don't explicitly specify an access modifier, C# assigns default access levels:

  • Top-level classes: internal
  • Class members (methods, fields): private
  • Nested types: private

Top-Level Classes: internal (Default)

When you define a top-level class without an access modifier, it will default to internal, meaning it can only be accessed within the same assembly (project).

// No access modifier specified, so it's implicitly 'internal'
class MyClass
{
// The class is accessible only within this assembly
public void DisplayMessage()
{
Console.WriteLine("Hello from MyClass!");
}
}


In this example, MyClass is implicitly internal, meaning it can't be accessed by other projects or assemblies that reference this project.


Class Members (Methods, Fields): private (Default)

If you don’t specify an access modifier for class members like fields or methods, they are implicitly private, meaning they are only accessible within the class.


class MyClass
{
// No access modifier specified, so it's implicitly 'private'
string message = "This is a private message"; // 'private' by default

// No access modifier specified, so it's implicitly 'private'
void ShowMessage()
{
Console.WriteLine(message); // Accessible within the class
}

// This public method can be called from outside the class
public void Display()
{
ShowMessage(); // ShowMessage() is 'private', but can be called inside the class
}
}


In this example, the field message and the method ShowMessage() are implicitly private. They can only be accessed within MyClass.


Nested Types: private (Default)

When a type (e.g., a class or struct) is nested within another class and no access modifier is specified, it defaults to private, meaning it can only be accessed from within the containing class.


class OuterClass
{
// No access modifier specified, so it's implicitly 'private'
class NestedClass
{
public void NestedMethod()
{
Console.WriteLine("This is a method in a private nested class");
}
}

public void AccessNestedClass()
{
NestedClass nested = new NestedClass(); // Can access the private nested class here
nested.NestedMethod();
}
}




In this example, NestedClass is implicitly private, meaning it can only be accessed within OuterClass.Access Modifiers Table

Conclusion

Access modifiers in C# allow you to control the visibility of your code. Understanding how to use them ensures that your code is organized, protected, and maintainable. By using access modifiers, you can limit what is exposed to other parts of your program or other developers, improving security and maintainability.

Here’s a quick summary:

  • public: Accessible anywhere.
  • private: Accessible only inside the class.
  • protected: Accessible inside the class and derived classes.
  • internal: Accessible within the same project.
  • protected internal: Accessible within the same project or derived classes in another project.
  • private protected: Accessible within the same project, but only in derived classes.
  • file: Accessible only within the same file.

Use access modifiers wisely to make your code clear, safe, and easier to manage.

Schedule a call now
Start your offshore web & mobile app team with a free consultation from our solutions engineer.

We respect your privacy, and be assured that your data will not be shared