3

I want to know the reasons for creating a class with only static methods, for example the Console or the Convert classes. The only reason I can think of is to group related functions together, so how is this different from a namespace.

  • 1
    static classes are basically a way to implement a singleton. Also, used to defined extensions to pre-existing classes. – Richard Schneider Feb 05 '15 at 11:46
  • Note that in C# every method, property, event etc. must live inside a type, like a `class`. It is not allowed to have a method as a direct member of a namespace. But I can see why you compare static classes to namespaces. – Jeppe Stig Nielsen Feb 05 '15 at 11:47
  • c# has one more usage: Extension methods must be declared in static classes. E.g.: System.Linq.Enumerable – ASh Feb 05 '15 at 11:47
  • @Richard So what exactly is the difference between a static class and a Singleton? The Singleton is only initialized when called, while the static class is initialized at start? – Kilazur Feb 05 '15 at 11:47
  • 1
    @Kilazur: See the answer on the comment below my answer. – Patrick Hofman Feb 05 '15 at 11:48
  • @Kilazur static class can not be inherited where on other side singleton can be inherited. – Jenish Rabadiya Feb 05 '15 at 11:51
  • possible duplicate of [When to Use Static Classes in C#](http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp) –  Feb 05 '15 at 11:55
  • @RichardSchneider _"static classes are basically a way to implement a singleton"_ - are you sure? _[A singleton allows access to a **single created instance** - that instance (or rather, a reference to that instance) **can be passed** as a parameter to other methods, and **treated as a normal object**. A static class allows only **static methods**.](http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern)_ –  Feb 05 '15 at 12:01

3 Answers3

6

There is just one console, so why do you need to instantiate a class? There is no need for it. That is just one reason. (This is just a way to implement the singleton design pattern)

Another reason could be that the methods are not related to an instance, they are just helper method, like Convert.XXX. Related to this are extension methods, that are obligated to be in a static class, also since they are not related to an instance of the class they are contained in.

Namespaces can't contain methods, so you need a class to wrap them in.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
3

A good answer to this is this interview to Stroustrup, talking about invariants. If a class doesn't modify any of its properties, or doesn't have a property you may want static methods.

http://slesinsky.org/brian/code/stroustrup_on_invariants.html

dariogriffo
  • 4,148
  • 3
  • 17
  • 34
0

You already have the point. Maybe it becomes clear when u see when we use such classes in our very big project cluster:

We use such a class e.g. as Utilities and Constants functionality in our project cluster.

That means you can use such utilities like as ConvertXFromYtoZ or WriteDebug anytime without taking care of the instance of the object.

We use also 'a lot' of different constants. Its very fine to have them in one static place: Constants.MyArea_Type_Foo.

By the way, it is also nice to have the static Regex.Match class. I nearly never use the dynamic Regex class.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325