
A private method cannot be called from outside its class. It can be called only from other class methods—both public and private. This promotes information hiding: programs become easier to maintain and test. Private is the default accessibility for members and methods. It is best used whenever possible.
KeywordsThis C# program uses private methods. Private restricts access to members.

This program declares two classes, one containing private methods and one containing the Main entry point. The program's goal is to demonstrate how the private modifier on a method affects how the method can be invoked. The program calls private methods from inside public method bodies. Also, the program demonstrates instance methods and also a private static method.
Program that uses private methods [C#]
using System;
class Test
{
private int Compute1()
{
return 1; // Private instance method that computes something.
}
public int Compute2()
{
return this.Compute1() + 1; // Public instance method.
}
private static int Compute3()
{
return 3; // Private static method that computes.
}
public static int Compute4()
{
return Compute3() + 1; // Public static method.
}
}
class Program
{
static void Main()
{
// Create new instance of the Test class.
// ... You can only call the Compute2 public instance method.
Test test = new Test();
Console.WriteLine(test.Compute2());
// Call the public static method.
// ... You cannot access the private static method.
Console.WriteLine(Test.Compute4());
}
}
Output
2
4
Description. This program is contained in a compilation unit and it contains two classes. The Test class introduces four method declarations to the program. The Test class contains two private methods and two public methods. From the Program class, which is separate from the Test class, you can only access the public methods.
Public MethodCalling private methods. The program also demonstrates how you can invoke a private method from inside a public method. This means that the private accessibility only affects how external sources can access the class, not how methods inside the class can access other methods.
Note: The accessibility of a public method is not transitive through the public method invocation. The private accessibility is lexically based, meaning it affects the members based on the position in the source text and type hierarchy.

The private modifier is part of the grammar of the C# language specification and it can be specified on many different parts of the syntax. You can create private classes, private fields, and private properties. This article focuses on private methods. In many programs, using the accessibility modifier on a member as the first modifier in the source text is desirable, as it makes the accessibility more discoverable.

The C# language automatically considers methods to be both instance methods (not static) and private methods (not public). This means that the private keyword is implicit on all method declarations, and you can only change that by specifying another accessibility such as public or protected. Sometimes, you can specify private to emphasize that the method should not be public.
Note: The system adopted by the C# language reduces the symmetry in the source text of programs.

We looked at method that are private in the C# language, seeing how they can be invoked from other class hierarchies and also how accessibility is not transitive but is instead based on the lexical scope of the program. This means that a private method can be called from a public method body. Private is implicitly added to all methods that do not declare an alternative modifier.
Method Tips