11

Possible Duplicate:
When to Use Static Classes in C#

Can someone please provide guidelines , standard checkpoints on when to create static class and when to create instance class.

Basically many a times I see while writing a code that the same thing could have been done using static class and methods and i get confused with many things.

So far i know below check points :

  1. If object under the consideration can exists only once in entire application then it would come under "static"

  2. If a method does not use any field variable inside it then it can be static method.

Community
  • 1
  • 1
Dhananjay
  • 3,673
  • 2
  • 22
  • 20
  • 1
    Does this help? http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp – kendaleiv Mar 17 '12 at 04:06
  • I've blogged recently about this matter http://www.sapiensworks.com/blog/post/2012/03/02/When-To-Use-The-Static-Keyword-In-Net.aspx – MikeSW Mar 17 '12 at 07:21

3 Answers3

27

To my mind the best differentiator is state.

If your class has no state and is just a series of related methods that, when given an input, will give you an output then you have an excellent candidate for a static class.

If your class will be given input and then store it or in some other way will be expected to maintain its current state then you have a regular class.

Furthermore if your class is going to be declared abstract or is going to inherit behaviours from another class or implement behaviours from an interface, then you're moving away from a static class and have a normal class.

In short:

  • If it's a bag of methods or has no state
  • and it isn't abstract, inheriting or implementing an interface

Then you have a good chance it is a static class :)

I hope that helps.

NB (as per the astute comment below) state refers to the class storing some data that represents the current state-of-affairs for an object. Perhaps a class-level variable or a property.

  • 1
    sounds really good. sometimes it's difficult to newbies to know the meaning of state.. lets say class fields. so restating the same : 1.class having no field variables declared, 2. class needs to be part of inheritance hierarchy. are points which will make class a static. – Dhananjay Mar 17 '12 at 05:10
  • Excellent addition, thank you @dnkulkarni. I'll amend my answer :) –  Mar 17 '12 at 05:55
  • A static class can have static variables.Also you might want to make a class static if you don't want instances of it, although static classes with static methods are way to easy abused by developers with a procedural mindset who love global variables and global functions. – MikeSW Mar 17 '12 at 07:30
  • @MikeSW I tend to view static classes with static variables as a code smell - it's indicative of a class that wants to have a state, ie be instantiable. All just IMO of course, naturally nothing is ever 100% :) –  Mar 17 '12 at 08:35
  • @KarlLynch, yes probably in over 95.27% of cases there is no need for static classes, but for some particular cases they can't be avoided. For example, I've coded a SingletonRegistry class for the time when an IoC container is too much and I still want to avoid the Singleton pattern, and that can only be a static class with static fields. – MikeSW Mar 17 '12 at 09:43
2

if it should be a collection of functions, then you'd use static classes. like :

int result = calculation.add(int p1,int p2);
float result = calculation.divide(int p1,int p2);

you can't say this on static classes:

Calculation mycalculator = new Calculation();

because they can not be instantiated. They can only contain static members.

Taken from here.

but if it'd be a member of a code, like apple in a tree, you'd use instance classes. each apple would have its own characteristics but it doesn't change the fact that it is an apple. and it's a part of the tree which contains multiple instances of the apple.

Apple apple1 = new Apple();
apple1.color = green;
apple1.age = newborn;
apple1.fallfromtree(speed);

Apple apple2 = new Apple();
apple2.color = red;

etc.

You can look here.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
1

Static Classes are best used to create libraries of functions that will be called. Regular Classes are used to create objects that can be manipulated. It allows you to encapsulate the relations of a single bunch of code. Take, for instance, a point in Euclidian space. You could declare two floats and keep track of them individually, or you could use the following contract to declare how a point works.

class Point{
    float x;
    float y;

    Point(float x, float y);

    Point halfway(Point other);

    Line lineThrough(Point other);

    float distanceFrom(Point other);

    ...
}

This is a bit redundant, but it allows you to to write more readable code in the future - better keeping all your data encapsulated and well designed, making you a better programmer, because the equation of a line through two points goes from

float slope = (xone-xtwo)/(yone-ytwo);
float yintercept = yone-slope*xone;

to

Point p1 = new Point(xone, yone);
Point p2 = new Point(xtwo, ytwo);
Line linethrough = p1.lineThrough(p2);

Sorry the pseudocode is a bit Java informed. I have Java on the brain.

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37