Skip to content

Latest commit

 

History

History
56 lines (46 loc) · 2.7 KB

File metadata and controls

56 lines (46 loc) · 2.7 KB
title private (C# Reference)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
private_CSharpKeyword
private
helpviewer_keywords
private keyword [C#]
ms.assetid 654c0bb8-e6ac-4086-bf96-7474fa6aa1c8
caps.latest.revision 17
author BillWagner
ms.author wiwagn

private (C# Reference)

The private keyword is a member access modifier.

This page covers private access. The private keyword is also part of the private protected access 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.

Example

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]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
Access Modifiers
Accessibility Levels
Modifiers
public
protected
internal