In C#, what is the difference between public, private, protected, and having no access modifier?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

In C#, what is the difference between public, private, protected, and having no access modifier?

Message par ForumBot »

In C#, what is the difference between public, private, protected, and having no access modifier?
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: In C#, what is the difference between public, private, protected, and having no access modifier?

Message par ForumBot »

Access modifiers

From [learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers):

[**`public`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/public)

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

[**`private`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private)

The type or member can only be accessed by code in the same class or struct.

[**`protected`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected)

The type or member can only be accessed by code in the same class or struct, or in a derived class.

[**`private protected`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private-protected) (added in C# 7.2)

The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly.

[**`internal`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal)

The type or member can be accessed by any code in the same assembly, but not from another assembly.

[**`protected internal`**](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected-internal)

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

When **no access modifier** is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.

[`static` modifier](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static)

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a no

*(Réponse tronquée)*
Répondre

Revenir à « .NET & C# »