
Information hiding improves software quality. The public modifier leaves nothing hidden. Used on members, classes and method declarations—both instance and static—public is not the default accessibility in the C# language. Instead it designates only those things that must be available to code outside the class.
KeywordsThis C# program uses public. It shows public methods and a public class.

This program demonstrates how you can use public methods on a class and then call those methods from outside the class. The public keyword is an accessibility modifier in the C# language. Methods are by default private and not public, so you must specify this modifier to call the methods from other parts in your program.
Public methods can be static, meaning attached to the type itself, or instance, meaning attached to an instance of the type that was constructed.
Static MethodProgram that uses public methods [C#]
using System;
public class Example
{
static int _fieldStatic = 1; // Private static field
int _fieldInstance = 2; // Private instance field
public static void DoStatic()
{
// Public static method body.
Console.WriteLine("DoAll called");
}
public static int SelectStatic()
{
// Public static method body with return value.
return _fieldStatic * 2;
}
public void DoInstance()
{
// Public instance method body.
Console.WriteLine("SelectAll called");
}
public int SelectInstance()
{
// Public instance method body with return value.
return _fieldInstance * 2;
}
}
class Program
{
static void Main()
{
// First run the public static methods on the type.
Example.DoStatic();
Console.WriteLine(Example.SelectStatic());
// Instantiate the type as an instance.
// ... Then invoke the public instance methods.
Example example = new Example();
example.DoInstance();
Console.WriteLine(example.SelectInstance());
}
}
Output
DoAll called
2
SelectAll called
4
Description. This program contains two classes: the Example class, which contains the public methods we are examining, and the Program class, which contains the Main entry point. There are four public methods in the Example class: two are static methods, two are not static methods, and half of them are void methods with no return values.
In the Main entry point, we see how you can call the DoStatic and SelectStatic methods through the type itself with no instance variable. The Example class can also be created with the new operator and the DoInstance and SelectInstance methods invoked this way.
Types versus instances versus objects. It is important to know the difference between types and instances in programming languages. A type can be thought of as a template from which instances and objects can be created. The type stores no state; it can specify the layout of objects that can store state.

Static fields and static data are not part of the type, and can be thought of as an instance that cannot be created more than once. The .NET Framework's execution engine is at its core object-oriented, to the level of the low-level instructions.

In the C# programming language, methods are implicitly considered to be private and instance methods. You must specify any deviations from this default state; any method that needs to be public must declare the 'public' modifier and any method that needs to be static (type-based) must declare the 'static' modifier. This default encourages the practice of information hiding in programming, which improves software quality.
Private Method
This example explored the public modifier as it applies to instance and static methods in the C# programming language. We noted how methods are not by default public and this modifier must be explicitly specified. Too many public methods indicate a poor design. Separate classes with more private methods is better. It promotes information hiding.
Method Tips