8

I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class."

So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyone guide me in which real time scenario we go for static class.

Some time in classes(not static) I am finding static methods. What advantage/perfomance advantage do static methods give over instance methods in practical.

Finglas
  • 15,518
  • 10
  • 56
  • 89
  • Interesting related questions : http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it http://stackoverflow.com/questions/731763/should-c-methods-that-can-be-static-be-static http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c – Jla Feb 09 '10 at 12:45

6 Answers6

20

For utility classes they are great. As you mentioned, they are similiar to global state. So for classes which have no state, for performance benefits the class should be static.

On the other hand, static classes are hard to test (if they contain state). Polymorphism and other OO concepts are also lost.

Use wisely.

Finglas
  • 15,518
  • 10
  • 56
  • 89
  • You should note, the performance gain I mentioned is trivial. – Finglas Feb 09 '10 at 09:02
  • C# has method hiding via the new keyword, which while it's not the same I have found one use for it in my C# work. Even still, it's dirty and being a TDDer, statics are something I avoid unless it falls into the utility class segment. – Finglas Feb 09 '10 at 09:17
  • "Utility classes" shouldn't be static. FileInfo is very much a utility class, it isn't static. Maybe utility methods. – Hans Passant Feb 09 '10 at 10:41
6

Applying the static keyword to a class is a C# language convention, it doesn't mean anything special to the CLR. It merely makes sure that all members are static as well and that you can't accidentally create an instance of the class with the new keyword.

The merits of static methods are discussed in this thread.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

IMO static classes are procedural programming in disguise. Not necessarily a bad thing, but it's not very OOPly. Watch out for the functional decomposition antipattern.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
1

Static classes are great for defining static methods. This is classic 'utility class' approach. However, be extremely careful with storing state (i.e. defining fields) in a static class. In our multi-threaded world this can lead to unpredictable program behavior unless you synchronize access to static fields.

Kerido
  • 2,930
  • 2
  • 21
  • 34
0

Static Classes globalizes a particular variable which makes it easier to handle during the code. Hence at basic level we prefer using Static Classes.

Shubham Das
  • 1,257
  • 11
  • 10
0

Static properties mainly used to introduce Context of running code. And you can find confirmation for that in every piece of .NET stack.
ASP.NET - HttpContext.Current
Threading - Thread.CurrentThread
WinForms - WindowsFormsSynchronizationContext.Current
WPF - Dispatcher
etc
static class for me is just container for utility methods.

Sergey Mirvoda
  • 3,209
  • 2
  • 26
  • 30