TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

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, internalKeywords
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.

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 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
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf