TheDeveloperBlog.com


C# Private Method

Private. A private method cannot be called from outside its class. It can be called only from other class methods—this promotes information hiding. Programs become easier to maintain and test. Private is the default accessibility.


Example. 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.

Note: The program calls private methods from inside public method bodies. It shows instance methods and a private static method.

Static Method
C# program that uses private methods

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

In this example, the Test class has four method declarations. 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 Method

The program also invokes 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.

Also: The private accessibility is lexically based—it affects the members based on the position in the source text and type hierarchy.


Accessibility. 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.

In many programs, using the accessibility modifier on a member as the first modifier in the source text is desirable. This makes the accessibility more discoverable. As always, please adhere to any existing project guidelines.


Implicit private. 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.

Tip: You can sometimes 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.


Summary. We used methods that are private. These can be invoked from other class hierarchies. Accessibility is not transitive but is instead based on lexical scope. This means that a private method can be called from a public method body.

Tip: Private is implicitly added to all methods that do not declare an alternative modifier.