---
title: "What are the access modifiers in C# and how do they control the visibility of class members?"  
description: "What are the access modifiers in C# and how do they control the visibility of class members?"  
author: "Utpal Vishwas"  
published: 2023-07-02  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/158926/what-are-the-access-modifiers-in-c-sharp-and-how-do-they-control-the-visibility-of-class-members  
category: "oops"  
tags: ["c#", "oops", "access modifiers"]  
reading_time: 2 minutes  

---

# What are the access modifiers in C# and how do they control the visibility of class members?

What are the [access modifiers](https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java) in C# and how do they [control the visibility](https://www.mindstick.com/forum/158658/what-is-the-css-property-used-to-control-the-visibility-or-hidden-state-of-an-element) of [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) members?

## Replies

### Reply by Aryan Kumar

In C#, [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) modifiers are keywords that [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) the [visibility](https://www.mindstick.com/blog/301300/how-to-improve-your-visibility-in-zero-click-search-results) of class members. They can be used to restrict access to class members, such as fields, properties, methods, and events.

There are five access [modifiers in C#](https://answers.mindstick.com/qa/82443/describe-accessibility-modifiers-in-c-sharp):

- **Public:** Public members are accessible from anywhere in the program.
- **Protected:** Protected members are accessible from within the class and from derived classes.
- **Internal:** Internal members are accessible from within the assembly.
- **Private:** Private members are only accessible from within the class.
- **Protected internal:** Protected internal members are accessible from within the class, from derived classes, and from other classes in the same assembly.

The default access modifier for class members is `private`. This means that class members are only accessible from within the class. To make a class member accessible from outside the class, you can use one of the other access modifiers.

For example, the following code defines a class with a public field:

C#

```plaintext
public class MyClass
{
    public int MyField;
}
```

The `MyField` field is public, which means that it can be accessed from anywhere in the program. For example, the following code creates a `MyClass` object and assigns the value 10 to the `MyField` field:

C#

```plaintext
MyClass myClass = new MyClass();
myClass.MyField = 10;
```

The `MyClass` class can also be inherited from other classes. If a derived class inherits from the `MyClass` class, it can access the `MyField` field.

Access modifiers can be used to control the visibility of class members and to protect sensitive data. By using access modifiers, you can make your code more secure and reusable.


---

Original Source: https://www.mindstick.com/forum/158926/what-are-the-access-modifiers-in-c-sharp-and-how-do-they-control-the-visibility-of-class-members

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
