8

Possible Duplicate:
What is the use of a static class

What are the benefits of declaring a static class?

public static class MyStaticClass
{
}

Are there any other than "it can't be instantiated"?
Why else would you want a static class, for what reasons should a class be static?
Is "any class should be declared as static unless it's meant to be instantiated" a good rule of thumb?

Community
  • 1
  • 1
bevacqua
  • 47,502
  • 56
  • 171
  • 285

5 Answers5

9

Static classes are useful as containers for utility functions and constants. If the class does not represent an object and is used only for this purpose then it makes sense to declare it as static.

sorpigal
  • 25,504
  • 8
  • 57
  • 75
8

If a class has the static modifier then:

  • It makes your intention clear: this isn't just a class which happens to have all static members - it's a class which will only ever have static members
  • You prevent instantiation without needing a private constructor just to suppress the default public one
  • You aren't allowed to declare a variable of that type
  • You aren't allowed to use that type as a type argument (IIRC, anyway)
  • You can declare extension methods in the type if it's a top-level type

The first point is the most important though, IMO. You're communicating intent to both the compiler and other developers.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • In a `.NET` project is it ok to create a static class to pass variable values between pages or there is a risk involved here. I heard we should avoid using global variables and static classes unless the context is very clear. In my case I'm using a static class to just assign a value of a variable from page1 (say, myStaticClass.City = "London") and then access it from page2 as myStaticClass.City. In such a simple case (with just a couple of class members) is it ok to use Static class just for sole purpose of passing variable values between pages of the the same app? – nam Nov 06 '20 at 21:14
  • 1
    @nam: Pages in what kind of app? For a web app, that's *definitely* a bad idea - it doesn't handle scaling to multiple servers, and it doesn't keep data separate for different users. Within a desktop app I would say it's still not a clean or testable design, but it doesn't have the same kind of problems. – Jon Skeet Nov 07 '20 at 07:44
  • Sorry, I forgot to mention `WPF app`. But forgetting, in fact, turned out to be good for me since your response even covered the web apps, as well (thank you). I'm getting an impression that using a static class in not necessarily a bad practice but [should be used wisely](https://stackoverflow.com/a/1315280/1232087). – nam Nov 07 '20 at 14:50
  • 1
    @nam: Using a static class *to store global mutable state* (which is what you're proposing) is usually a bad idea IMO. Global state is hard to reason about and hard to test. Static classes that only store immutable state and contain state methods are fine IMO. – Jon Skeet Nov 07 '20 at 15:18
4

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

Taken from MSDN directly. It explains it much better than I can. The key fact here is that the object persists throughout the life.

Extention methods also have to be written in static classes. I tend to use these for string extentions, or collection extentions.

jimplode
  • 3,474
  • 3
  • 24
  • 42
3

You need a static class to create extension methods.

hoang
  • 1,887
  • 1
  • 24
  • 34
0

An utility class, which only declares static methods (like System.IO.File) is not ment to be instantiated, so its usually static.

And you're right, I find it to be a good use. Singleton is different, as it is meant to be privately instanciated.

awe
  • 21,938
  • 6
  • 78
  • 91
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54