C# Public and private MethodsUse the public and private keywords. Describe information hiding and object-oriented programming.
Public and private. A public member can be called from external locations. Used on members, classes and method declarations, public is not the default accessibility.
a private method cannot be called from outside its class. It can be called only from other class methods—this promotes information hiding.Protected, internal
We show how to define public methods. We then call those methods from outside the class. Public is an accessibility modifier. It is not the default, which is private.Class
Static: Public methods can be static. This means they are attached to the type itself.StaticType
Also: They can be instance—this means they are attached to an instance of the type that was constructed.
C# program that uses public methods
public class Example
static int _fieldStatic = 1; // Private static field
int _fieldInstance = 2; // Private instance field
public static void DoStatic()
// Public static method body.
public static int SelectStatic()
// Public static method body with return value.
return _fieldStatic * 2;
public void DoInstance()
// Public instance method body.
public int SelectInstance()
// Public instance method body with return value.
return _fieldInstance * 2;
static void Main()
// First run the public static methods on the type.
// Instantiate the type as an instance.
// ... Then invoke the public instance methods.
Example example = new Example();
This program shows how the private modifier (on a method) affects how the method can be invoked. It shows instance methods and a private static method.
Advantages: With private fields, programs become easier to maintain and test. Private is the default accessibility.
Test: Test has 4 method declarations: 2 private methods and 2 public methods. From Program, you can only access the public methods.
C# program that uses private methods
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.
static void Main()
// Create new instance of the Test class.
// ... You can only call the Compute2 public instance method.
Test test = new Test();
// Call the public static method.
// ... You cannot access the private static method.
Note, private and public.
We can invoke a private method from inside a public method. Private accessibility only affects how external sources can access the class.
Info: 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.
Private and public are part of the grammar of the C# language specification. They can be specified on many parts of the syntax.
Note: In many programs, using the accessibility modifier on a member as the first modifier in the source text is desirable.
Note 2: This makes the accessibility more discoverable. As always, please adhere to any existing project guidelines.
The C# language automatically considers methods to be both instance methods (not static) and private methods (not public). The private keyword is implicit on all methods.
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.
Methods are implicitly private and instance. You must specify any deviations from this default state. Any method that needs to be public must declare the public modifier.
Note: This default encourages the practice of information hiding in programming. And this improves software quality.
We considered public and private things in C#. A private method 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. Private is implicitly added to all methods that do not declare an alternative modifier.
© 2007-2020 sam allen. send bug reports to email@example.com.