HomeSearch

C# Public and private Methods

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

Meanwhile,

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

Public example.

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.

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

Notes, types.

Consider the difference between types and instances. A type can be thought of as a template from which instances and objects can be created.

Tip: The type stores no state. It can specify the layout of objects that can store state.

Type

Static: 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.

Static

Thus: The .NET Framework's execution engine, even its low-level instructions, is object-oriented.

Private 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 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

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.

Accessibility.

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.

Implicit private.

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.

Default.

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.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.