In C#, there are five accessibility modifiers: public, private, protected, internal, and protected internal. These modifiers control the level of access that a class, method, property, or field has to other classes or code within the same assembly or outside of it.
Public: A public member is accessible from any code within the assembly or outside of it. This modifier is commonly used for methods, properties, and fields that need to be accessed by other code.
Example:
public class MyClass {
public int MyProperty { get; set; }
public void MyMethod() {
// do something
}
}
Private: A private member is only accessible from within the same class or struct. This modifier is used to encapsulate implementation details and hide them from other code.
Example:
public class MyClass {
private int myField;
private void MyMethod() {
// do something
}
}
Protected: A protected member is accessible from within the same class or struct, as well as any derived class. This modifier is used to expose implementation details to derived classes while still encapsulating them from other code.
Example:
public class MyBaseClass {
protected int myField;
protected void MyMethod() {
// do something
}
}
public class MyDerivedClass : MyBaseClass {
public void AnotherMethod() {
myField = 10; // can access the protected field from the base class
MyMethod(); // can call the protected method from the base class
}
}
Internal: An internal member is accessible from any code within the same assembly, but not from code outside of it. This modifier is used to expose implementation details to other code within the same assembly.
Example:
internal class MyClass {
internal int MyField;
internal void MyMethod() {
// do something
}
}
Protected internal: A protected internal member is accessible from within the same assembly, as well as any derived class whether inside or outside of the assembly. This modifier is used to expose implementation details to derived classes within the same assembly, as well as to other code within the same assembly.
Example:
public class MyBaseClass {
protected internal int myField;
protected internal void MyMethod() {
// do something
}
}
public class MyDerivedClass : MyBaseClass {
public void AnotherMethod() {
myField = 10; // can access the protected internal field from the base class
MyMethod(); // can call the protected internal method from the base class
}
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, there are five accessibility modifiers: public, private, protected, internal, and protected internal. These modifiers control the level of access that a class, method, property, or field has to other classes or code within the same assembly or outside of it.
Public: A public member is accessible from any code within the assembly or outside of it. This modifier is commonly used for methods, properties, and fields that need to be accessed by other code.
Private: A private member is only accessible from within the same class or struct. This modifier is used to encapsulate implementation details and hide them from other code.
Example:
Protected: A protected member is accessible from within the same class or struct, as well as any derived class. This modifier is used to expose implementation details to derived classes while still encapsulating them from other code.
Example:
Internal: An internal member is accessible from any code within the same assembly, but not from code outside of it. This modifier is used to expose implementation details to other code within the same assembly.
Example:
Protected internal: A protected internal member is accessible from within the same assembly, as well as any derived class whether inside or outside of the assembly. This modifier is used to expose implementation details to derived classes within the same assembly, as well as to other code within the same assembly.
Example: