C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Static: Public methods can be static. This means they are attached to the type itself.
StaticTypeAlso: They can be instance—this means they are attached to an instance of the type that was constructed.
C# program that uses public methods
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
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
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
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.
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.
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.
Note: This default encourages the practice of information hiding in programming. And this improves software quality.