In C#, access specifiers (also called access modifiers) control the visibility and accessibility of classes, methods, properties, and other members. They define who can access a class or member. Here’s a clear explanation for your forum answer:
1. public
Accessible from anywhere in the project or other assemblies.
public class MyClass
{
public int MyValue; // accessible anywhere
}
Use it when you want the member to be visible everywhere.
2. private
Accessible only within the containing class.
class MyClass
{
private int myValue; // accessible only inside MyClass
}
Default for class members. Use for encapsulation.
3. protected
Accessible within the containing class and derived classes (inheritance).
class BaseClass
{
protected int myValue;
}
class DerivedClass : BaseClass
{
void Test()
{
myValue = 10; // OK
}
}
4. internal
Accessible only within the same assembly/project.
internal class MyClass
{
internal int myValue;
}
Useful for library code that should not be exposed outside the project.
5. protected internal
Accessible from derived classes or within the same assembly.
protected internal int myValue;
Combines protected + internal.
6. private protected (C# 7.2+)
Accessible only within the containing class or derived classes in the same assembly.
Sandhiya PriyaPosted Oct 13, 2025, 7:01 AM
In C#, access specifiers (also called access modifiers) control the visibility and accessibility of classes, methods, properties, and other members. They define who can access a class or member. Here’s a clear explanation for your forum answer:
1.
publicAccessible from anywhere in the project or other assemblies.
Use it when you want the member to be visible everywhere.
2.
privateAccessible only within the containing class.
Default for class members. Use for encapsulation.
3.
protectedAccessible within the containing class and derived classes (inheritance).
4.
internalAccessible only within the same assembly/project.
Useful for library code that should not be exposed outside the project.
5.
protected internalAccessible from derived classes or within the same assembly.
Combines
protected+internal.6.
private protected(C# 7.2+)Accessible only within the containing class or derived classes in the same assembly.
More restrictive than
protected internal.Quick Summary Table
privateprotectedinternalprotected internalprivate protectedpublicCynthia SathuragiriPosted Sep 18, 2025, 5:23 AM
In C#, access specifiers (or access modifiers) decide who can use a class, method, or variable.
Here are the main ones:
public → Anyone can access it.
private → Only inside the same class.
protected → Inside the same class + child (derived) classes.
internal → Only inside the same project/assembly.
protected internal → Inside the same project + child classes (even if child is in another project).
private protected → Inside the same class + child classes, but only if they’re in the same project.
Jignesh KumarPosted Sep 18, 2025, 2:53 AM
Hello Mickel,
Please refer this common oops interview artile where I have explained access specifier in detail with example,
Access Specifier in C# with Example
Sangeetha SPosted Sep 17, 2025, 1:12 PM
public class Car
{
private string engineNumber; // Only accessible within Car
protected int speed; // Accessible in Car and derived classes
internal string model; // Accessible within the same assembly
protected internal string color; // Accessible in same assembly or derived classes
private protected string ownerName; // Accessible in same assembly and derived classes only
public void StartEngine() // Accessible from anywhere
{
Console.WriteLine("Engine started");
}
}