23

I'm new to C#.Till this moment I used to make every global variable - public static.All my methods are public static so I can access them from other classes.

I read on SO that the less public static methods I have,the better.So I rewrote my applications by putting all the code in one class - the form class.Now all my methods are private and there's no static method.

My question: What should I do,keeping everything in the form class is dump in my opinion.

When should I use public,when private and when static private/public?

I get the public methods as a 'cons' ,because they can be decompiled,but I doubt that.My public methods can be decompiled too.What is so 'private' in a private method?

EDIT: I'm not asking how to prevent my program to be decompiled,I'm asking whether I should use static,private and public.And also : Is there are problem in putting all the code in the form class so I dont have to use public methods?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
  • Your question seems like a duplicate of: [http://stackoverflow.com/questions/99688/private-vs-public-members-in-practice-how-important-is-encapsulation](http://stackoverflow.com/questions/99688/private-vs-public-members-in-practice-how-important-is-encapsulation) – Adam Robinson Apr 27 '09 at 13:33

7 Answers7

16

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

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
10

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

TStamper
  • 30,098
  • 10
  • 66
  • 73
5

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

lee-m
  • 2,269
  • 17
  • 29
Tom
  • 51
  • 1
  • 1
4

See Access Modifiers (C# Programming Guide). But it's be far better if you got yourself a decent C# and OOP/OOD book: these are really basics of computer science.

Long story short: access modifiers promote encapsulation, which basically means that every class should keep its privates to itself.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

Visible to the package, the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Here is an example:

public class Bicycle {

private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBicycles = 0;

public Bicycle(int startCadence, int startSpeed, int startGear){
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;

   id = ++numberOfBicycles;
}

// new method to return the ID instance variable
public int getID() {
    return id;
}
    ...

}

0

In addition to the above, think about working scenario. You are not working alone. Your peer fellow might call the methods from your class which you actually don't want to expose, or think it needs critical changes in the future. That might be the case you want a private

0

This seems more basic than the question linked above. To properly thrive in an OO language, you'll need to figure out how to decompose your end goal into a series of objects that work together (even containing and extending one another) to achieve a series of goals. This abstraction has numerous benefits which become apparent once you begin properly implementing OO design. You're going to want a new C# book, as mentioned, if you haven't already gotten to the part explaining the basics of Object Oriented Programming.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406