Singleton vs static

  • Thread starter Anders Eriksson
  • Start date
A

Anders Eriksson

I wonder what's the difference between a Singleton class and a Static class?

// Anders
 
T

Tony Johansson

Peter Duniho said:
You mean other than the obvious?

I would say that the difference most likely to be pertinent in most
scenarios is that a singleton can implement an interface. In also can
inherit a class other than System.Object, and be inherited by another
class, while a static class cannot do any of those things.

Pete

As I have learned is a singelton a normal instance class that exist in only
one copy but a static class is a class that is accessed by using the type
itself

//Tony
 
A

Arne Vajhøj

As I have learned is a singelton a normal instance class that exist in only
one copy but a static class is a class that is accessed by using the type
itself

That is the syntax difference from the caller perspective.

SingletonClass.Foobar

vs

SingletonClass.Instance.Foobar

That is different from why pick one over the other.

Arne
 
A

Arne Vajhøj

I wonder what's the difference between a Singleton class and a Static
class?

I would say that the biggest difference is in what they are
intended for.

A singleton class is a real OO class that can be instantiated, but
is designed so that only one instance can exist within the
app.

A static class is a non-OO class that is intended for
procedural programming style.

Well - it is also used for extension methods, which is OO'ish,
but that is just MS's pick of syntax for this featre.

By making your choice you send a signal to maintenance
programmers what the class is.

Besides that a singleton class can implement an interface,
which can be useful.

A singleton class can also participate in
(implementation) inheritance, but I would strongly
recommend you not to try that. It is usually a very
painful experience.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top