नोट
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप साइन इन करने या निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
Tip
This article is part of the Fundamentals section for developers who already know at least one programming language and are learning C#. If you're new to programming, start with the Get started tutorials first.
Coming from another language? If you used Java access modifiers, you recognize public and private.
Accessibility determines which code can use a type or member. An access modifier is a keyword, such as public or private, that specifies that access. The access modifiers reference describes all supported combinations.
These are the most common access modifiers:
privatelimits access to the containing type.publicallows access from any code, provided the containing type is also accessible.internallimits access to the same assembly, a compiled unit of .NET code, usually a.dllor.exefile. A C# project normally produces one assembly. See Assemblies in .NET.protectedlimits access to the containing class and its derived classes, the classes that inherit from it, even in another assembly.
Separate public capabilities from implementation
Use public for the members you want callers to use and private for implementation details used inside the class. These public members form the class's public interface, the operations and values it exposes to callers. Here, "interface" describes the class's exposed members, rather than a separate C# interface type. For example, you can expose the counter's operations and keep its fields private:
public class VisitCounter
{
private int count;
private static int total;
public void AddVisit()
{
count++;
total++;
}
public int GetCount() => count;
public static int GetTotal() => total;
public static int GetTotalExcluding(VisitCounter counter) =>
total - counter.count;
}
Through the public methods, you can add a visit or read a count. Direct access to the private fields stays inside VisitCounter:
var counter = new VisitCounter();
counter.AddVisit();
Console.WriteLine(counter.GetCount()); // => 1
Accessibility lets you limit the scope of where change can happen. Keep fields and helper methods private when they're implementation details, so you can change them within the class while preserving its public interface. Callers then depend on the operations you expose, rather than on how you implement them. In the counter, AddVisit keeps the instance count and total in sync.
Default accessibility
Access modifiers are optional. Every type or member has a default accessibility. Don't rely on the default accessibility in your code. Declare accessibility explicitly. Developers who read your code see your intent without having to memorize the defaults.
The default accessibility for a type depends on where it's declared. Start with a class declared directly in a namespace. A namespace groups names and imposes no access restriction of its own, so the class's declared accessibility determines where you can use it. The default accessibility for a class declared in a namespace is internal. The default accessibility on a member of class, including a nested type, is private. For example:
class LocalCounter
{
int count = 0;
public int GetCount() => count;
}
LocalCounter is internal, and count is private. Although GetCount is public, code in another assembly can't name LocalCounter to call it.
To access a member or nested type, your code must satisfy both its accessibility and its containing type's accessibility. You can apply the same rule recursively at every level of nesting: each containing type must also allow access. For example, a public nested type inside an internal class is available only where that containing class is accessible. See Accessibility domain for the full model.
A positional record class normally generates public properties for its parameters.
Other access modifiers
You'll encounter other accessibility modifiers as you learn more about classes. The Get started roadmap for Java developers introduces the familiar public and private keywords. Use the access modifiers reference for the complete set of choices.
As you work with inheritance, you'll use protected for access within a class and its derived classes. For combinations such as protected internal and private protected, consult the accessibility levels reference. For a type used only within one source file, see the file modifier.
Accessible members must also use sufficiently accessible types. For example, a public method on a public class can't return a private nested type. Otherwise, callers could reach a method whose result type they can't name. For signature constraints and specialized cases, see Accessibility restrictions.
Next steps
- Use Fields and constants for state declarations.
- Use Properties to expose values through accessors.
- Use the existing accessibility guide for other type kinds, friend assemblies, and inheritance-related access details.