Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use public/private/static methods? [closed]

Tags:

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?

like image 1000
Ivan Prodanov Avatar asked Apr 27 '09 13:04

Ivan Prodanov


2 Answers

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

like image 119
Stefano Borini Avatar answered Sep 27 '22 18:09

Stefano Borini


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

like image 32
TStamper Avatar answered Sep 27 '22 18:09

TStamper