Answered by:
Advantage of Singleton?

Question
-
User619554136 posted
See in below I have writtine a
1. Singleton class Emp1 with Display Method and
2. Static Class with Static Method Display.What is the advantage of singleton class over Static Class with Static members?
using System; namespace dp { class Program { static void Main(string[] args) { Emp1 e1 = Emp1.GetInstance(); Emp1 e2 = Emp1.GetInstance(); Console.WriteLine(ReferenceEquals(e1, e2)); e1.Display(); Emp2.Display(); } } class Emp1 { private static readonly Emp1 emp = new Emp1(); private Emp1() { } public void Display() { } public static Emp1 GetInstance() { return emp; } } class Emp2 { private Emp2() { } public static void Display() { } } }
Tuesday, March 30, 2010 9:05 AM
Answers
-
User-821857111 posted
With a singleton, you can pass that around like any object. A static class only allows static methods, and cannot be instantiated.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 30, 2010 3:09 PM -
User-525215917 posted
Static scope of classes should only provide functions that are external of all classes. Your business logic must be still implemented in instantiated classes. If you are able to avoid static instances of classes then do it. If you are sharing objects between threads you must handle also threading issues.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 31, 2010 1:40 AM -
-
User619554136 posted
My Quesion was why we need to use singleton instead static class and static methods. I mean what is the advatage of singleton class over a class which contains static methods.
Thanks for all for giving your answers. It helped me to prepare 3 advantages I wrote in my blog for my future reference.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 5, 2010 7:18 AM -
User-952121411 posted
C# - Singleton Pattern vs. Static Classes:
http://dotnet.dzone.com/news/c-singleton-pattern-vs-static-
C# Singleton Pattern Versus Static Class:
http://dotnetperls.com/singleton-static
Singletons vs. Static Classes:
http://leedumond.com/blog/singletons-vs-static-classes/
Singleton vs static:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2007-12/msg00740.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 6, 2010 11:12 AM
All replies
-
User-821857111 posted
With a singleton, you can pass that around like any object. A static class only allows static methods, and cannot be instantiated.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 30, 2010 3:09 PM -
User-525215917 posted
Static scope of classes should only provide functions that are external of all classes. Your business logic must be still implemented in instantiated classes. If you are able to avoid static instances of classes then do it. If you are sharing objects between threads you must handle also threading issues.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 31, 2010 1:40 AM -
-
User8761146 posted
And only with Singleton you can guarantee that only one instance of class exists.
Wednesday, March 31, 2010 11:22 AM -
User619554136 posted
I'm bit confusing on understand various replies on singleton over direct static methods. Just let you know that I shall take another day to provide my queries or mark as answered.
Thursday, April 1, 2010 4:38 PM -
User437720957 posted
And only with Singleton you can guarantee that only one instance of class exists.
Which is the original purpose of the pattern.
Sunday, April 4, 2010 6:41 PM -
User619554136 posted
My Quesion was why we need to use singleton instead static class and static methods. I mean what is the advatage of singleton class over a class which contains static methods.
Thanks for all for giving your answers. It helped me to prepare 3 advantages I wrote in my blog for my future reference.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 5, 2010 7:18 AM -
User-952121411 posted
C# - Singleton Pattern vs. Static Classes:
http://dotnet.dzone.com/news/c-singleton-pattern-vs-static-
C# Singleton Pattern Versus Static Class:
http://dotnetperls.com/singleton-static
Singletons vs. Static Classes:
http://leedumond.com/blog/singletons-vs-static-classes/
Singleton vs static:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2007-12/msg00740.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 6, 2010 11:12 AM -
User619554136 posted
Thanks for providing me more links.
Friday, April 16, 2010 6:00 AM