When should I use public/private/static methods?

private is for class members that you want only access within the class of the body, and in C# members are default set to private unless specified different

examples of when to use private:

class Account
{

  private int pin = 1090;
  public int Pin
  {
     get { return pin; }
  }
}

public on the other hand is the opposite, there are no restrictions with accessing public members, so when things that don't matter with the user having access to should be public.

static on the other hand has no relation to the two, because it doesn't deal with permission to methods, static on the other hand is a constant or type declaration. If the word static is applied to the class then every member in the class must be declared static.

examples of when to use static:

  static int birth_year= 1985

Modifiers in C# Reference will give you more detail of all modifiers in C# and examples of how they should be used


Everything should be private unless proven otherwise. The difference between public and private is between what is supposed to be kept compatible and what is not supposed to be kept compatible, what is supposed to be interesting to the world and what is not supposed to be its business.

When you declare something public, the class (and consequently the object) is making a strong statement: this is my visible interface, there are many other like this, but this is mine. The public interface is a contractual agreement that your class is exporting to the rest of the world (whatever that means) about what it can do. If you modify the public interface, you risk breaking the contract that the rest of the world is assuming about the class.

On the other hand, private stuff is internal to the class. It supports functionality that the class must use to do its job while carrying the object state around (if it's a method) or keeping its internal state (if it's a variable). You are free to hack and tinker the class private stuff as much as you want, without breaking the interface contract, meaning that this gives you wide freedom for refactoring (of the internal data representation, for example, for efficiency). Private stuff is not part of the interface.

Protected is something that involves the openness to reimplementation. Avoid, if you can, deeply nested inheritances. You risk making things very difficult to handle, as your reimplementation class can screw the base class.

Technically, a class should declare an interface (public) and an implementation (private). The interface should not have code at all, just delegate to the private "implementation" logic. This is why in Java and C# you have interface statement, which formalizes the pure abstract class concept in C++.

Static is something that resides logically in the realm of your class but does not depend on the state of the class itself. It should be used sparingly when a design pattern dictates it (e.g., singleton, factory method).


All is answered above already, but I think it could be simplified a bit... so decorate a method public, if other classes will be using this method. If not - mark it private.

For instance you have Class A and Class B. Say Class A has 3 methods (x,y,z). Methods x, y will be used by Class B, so mark them both public, but method z will be only be used by method x inside Class A so mark it private as there's no need to be exposing this method to the external world. The logic inside this method is intended for internal use only.

Static is differentl this decoration means you cannnot create an instance of an object marked static. The object is - as the keyword says - static (cannot be changed or modified).

Tags:

C#

Vb.Net