tags:

views:

776

answers:

6

Hello,

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?

+2  A: 

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
+3  A: 

I would start learning the basics about object oriented programming. There are lots of resources out there. Probably the best is to take a course to start. You seem to be far from understanding OO principles.

Stefan Steinegger
When I said "I'm new to C#" I didn't mean I'm newbie.I've been a pascal lover years ago,I'm just new to C#.
John
It's really just an advise. I didn't want to be offensive or saying that you are not a good programmer. But when you say you made "everything static" and "put everything into one class to make it private" sounds as if you were not familiar with object oriented programming.
Stefan Steinegger
A: 

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
+2  A: 

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

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 to break 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 is support functionality that the class must use in order 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 to make things very difficult to handle, as the base class can be screwed by your reimplementation 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 formalize 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 (eg. singleton, factory method).

Stefano Borini