| title | private (C# Reference) | ||
|---|---|---|---|
| ms.date | 07/20/2015 | ||
| ms.prod | .net | ||
| ms.technology |
|
||
| ms.topic | article | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | 654c0bb8-e6ac-4086-bf96-7474fa6aa1c8 | ||
| caps.latest.revision | 17 | ||
| author | BillWagner | ||
| ms.author | wiwagn |
The private keyword is a member access modifier.
This page covers
privateaccess. Theprivatekeyword is also part of theprivate protectedaccess modifier.
Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:
class Employee
{
private int i;
double d; // private access by default
}
Nested types in the same body can also access those private members.
It is a compile-time error to reference a private member outside the class or the struct in which it is declared.
For a comparison of private with the other access modifiers, see Accessibility Levels and Access Modifiers.
In this example, the Employee class contains two private data members, name and salary. As private members, they cannot be accessed except by member methods. Public methods named GetName and Salary are added to allow controlled access to the private members. The name member is accessed by way of a public method, and the salary member is accessed by way of a public read-only property. (See Properties for more information.)
[!code-csharpcsrefKeywordsModifiers#10]
[!INCLUDECSharplangspec]
C# Reference
C# Programming Guide
C# Keywords
Access Modifiers
Accessibility Levels
Modifiers
public
protected
internal