Author |
Message |
azamr #1 / 23
|
 singleton
Hi, What is the good way, in C# to make singleton objects ? Thank you.
|
Sun, 15 May 2005 00:21:41 GMT |
|
 |
Chris R. Timmon #2 / 23
|
 singleton
Quote: > Hi, > What is the good way, in C# to make singleton objects ?
public class SingletonClass { public static readonly SingletonClass Instance = new SingletonClass(); private SingletonClass() : base() { } Quote: }
Hope this helps. Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
|
Sun, 15 May 2005 00:37:38 GMT |
|
 |
Chad Myer #3 / 23
|
 singleton
Quote: > Hi, > What is the good way, in C# to make singleton objects ?
Here's a sample on MSDN: http://tinyurl.com/30wa It starts out with C++ but then shows how to do it in C#. A basic example would be: sealed class Singleton { private Singleton() {} public static readonly Singleton Instance = new Singleton(); Quote: }
It's thread safe, you can't extend from it, and no one can create an instance of it but the Singleton itself. -c
|
Sun, 15 May 2005 03:05:35 GMT |
|
 |
jame #4 / 23
|
 singleton
you forgot 'sealed' on your class
Quote:
> > Hi, > > What is the good way, in C# to make singleton objects ? > public class SingletonClass > { > public static readonly SingletonClass Instance = > new SingletonClass(); > private SingletonClass() : base() { } > } > Hope this helps. > Chris. > ------------- > C.R. Timmons Consulting, Inc. > http://www.crtimmonsinc.com/
|
Sun, 15 May 2005 02:56:19 GMT |
|
 |
Chris R. Timmon #5 / 23
|
 singleton
Quote: > you forgot 'sealed' on your class
Good catch. Thanks. Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
|
Sun, 15 May 2005 03:50:22 GMT |
|
 |
Chris McMah #6 / 23
|
 singleton
What is the advantage of this approach over using nothing but static methods and variables for a class (and making the constructor private)? Chris Quote:
> > Hi, > > What is the good way, in C# to make singleton objects ? > Here's a sample on MSDN: > http://tinyurl.com/30wa > It starts out with C++ but then shows how to do it in > C#. > A basic example would be: > sealed class Singleton > { > private Singleton() {} > public static readonly Singleton Instance = > new Singleton(); > } > It's thread safe, you can't extend from it, and no one can > create an instance of it but the Singleton itself. > -c
|
Sun, 15 May 2005 07:33:46 GMT |
|
 |
Chris R. Timmon #7 / 23
|
 singleton
Quote: > What is the advantage of this approach over using nothing but > static methods and variables for a class (and making the > constructor private)?
Chris, I use singletons when streaming the instance to/from an XML file. I haven't found an easy way to do that with a purely static class, so I just use the singleton instance. Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
|
Sun, 15 May 2005 11:20:32 GMT |
|
 |
Jeff Loui #8 / 23
|
 singleton
Chris... http://www.aspfree.com/authors/jeff_louie/OOP/twisted4.asp One of the advantages of the factory method is that you can modify the singleton behavior of the class without affecting the caller of the class. If you decide that your application should now support Kings present and past, then you can modify MyClass to return a new instance for each call to GetInstance. Here is the modified multi-instance version of MyClass: Regards, Jeff Quote: >What is the advantage of this approach over using nothing but
static methods and variables for a class (and making the constructor private)?< *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
|
Sun, 15 May 2005 12:33:01 GMT |
|
 |
Chad Myer #9 / 23
|
 singleton
If you have a class that is solely methods that are stateless, then you can just use static methods. However, if you have any state (member variables and whatnot), having a singleton can be beneficial. -c
Quote: > What is the advantage of this approach over using nothing but static > methods and variables for a class (and making the constructor > private)? > Chris
Quote:
> > > Hi, > > > What is the good way, in C# to make singleton objects ? > > Here's a sample on MSDN: > > http://tinyurl.com/30wa > > It starts out with C++ but then shows how to do it in > > C#. > > A basic example would be: > > sealed class Singleton > > { > > private Singleton() {} > > public static readonly Singleton Instance = > > new Singleton(); > > } > > It's thread safe, you can't extend from it, and no one can > > create an instance of it but the Singleton itself. > > -c
|
Sun, 15 May 2005 14:22:39 GMT |
|
 |
azamr #10 / 23
|
 singleton
Quote:
> A basic example would be: > sealed class Singleton > { > private Singleton() {} > public static readonly Singleton Instance = > new Singleton(); > } > It's thread safe, you can't extend from it, and no one can > create an instance of it but the Singleton itself.
Thank you for responses.
|
Sun, 15 May 2005 18:44:37 GMT |
|
 |
Chris McMah #11 / 23
|
 singleton
Static variables can store state. Chris Quote:
> If you have a class that is solely methods that are stateless, > then you can just use static methods. > However, if you have any state (member variables and whatnot), > having a singleton can be beneficial. > -c
> > What is the advantage of this approach over using nothing but static > > methods and variables for a class (and making the constructor > > private)? > > Chris
> > > > Hi, > > > > What is the good way, in C# to make singleton objects ? > > > Here's a sample on MSDN: > > > http://tinyurl.com/30wa > > > It starts out with C++ but then shows how to do it in > > > C#. > > > A basic example would be: > > > sealed class Singleton > > > { > > > private Singleton() {} > > > public static readonly Singleton Instance = > > > new Singleton(); > > > } > > > It's thread safe, you can't extend from it, and no one can > > > create an instance of it but the Singleton itself. > > > -c
|
Mon, 16 May 2005 02:02:04 GMT |
|
 |
Chris McMah #12 / 23
|
 singleton
That would certainly be an advantage to using an instance. Chris
Quote:
> > What is the advantage of this approach over using nothing but > > static methods and variables for a class (and making the > > constructor private)? > Chris, > I use singletons when streaming the instance to/from an XML file. > I haven't found an easy way to do that with a purely static class, > so I just use the singleton instance. > Chris. > ------------- > C.R. Timmons Consulting, Inc. > http://www.crtimmonsinc.com/
|
Mon, 16 May 2005 02:07:05 GMT |
|
 |
Chris McMaho #13 / 23
|
 singleton
Jeff, I think you forgot to post the code example. :) Chris Quote:
> Chris... > http://www.aspfree.com/authors/jeff_louie/OOP/twisted4.asp > One of the advantages of the factory method is that you can modify > the singleton behavior of the class without affecting the caller of > the class. If you decide that your application should now support > Kings present and past, then you can modify MyClass to return a > new instance for each call to GetInstance. Here is the modified > multi-instance version of MyClass: > Regards, > Jeff > >What is the advantage of this approach over using nothing but > static methods and variables for a class (and making the > constructor private)?< > *** Sent via Developersdex http://www.developersdex.com *** > Don't just participate in USENET...get rewarded for it!
|
Mon, 16 May 2005 02:10:57 GMT |
|
 |
Chris McMaho #14 / 23
|
 singleton
Jeff, I think you forgot to post the code example. :) Chris Quote:
> Chris... > http://www.aspfree.com/authors/jeff_louie/OOP/twisted4.asp > One of the advantages of the factory method is that you can modify > the singleton behavior of the class without affecting the caller of > the class. If you decide that your application should now support > Kings present and past, then you can modify MyClass to return a > new instance for each call to GetInstance. Here is the modified > multi-instance version of MyClass: > Regards, > Jeff > >What is the advantage of this approach over using nothing but > static methods and variables for a class (and making the > constructor private)?< > *** Sent via Developersdex http://www.developersdex.com *** > Don't just participate in USENET...get rewarded for it!
|
Mon, 16 May 2005 02:52:17 GMT |
|
 |
Chad Myer #15 / 23
|
 singleton
I realize that. However, there are some cases in which having an actual instance are necessary, none of which I can think of right now :) I just ate a huge BBQ dinner... tummy full... brain dysfunctional. -c
Quote: > Static variables can store state. > Chris
Quote: > > If you have a class that is solely methods that are stateless, > > then you can just use static methods. > > However, if you have any state (member variables and whatnot), > > having a singleton can be beneficial. > > -c
> > > What is the advantage of this approach over using nothing but static > > > methods and variables for a class (and making the constructor > > > private)? > > > Chris
> > > > > Hi, > > > > > What is the good way, in C# to make singleton objects ? > > > > Here's a sample on MSDN: > > > > http://tinyurl.com/30wa > > > > It starts out with C++ but then shows how to do it in > > > > C#. > > > > A basic example would be: > > > > sealed class Singleton > > > > { > > > > private Singleton() {} > > > > public static readonly Singleton Instance = > > > > new Singleton(); > > > > } > > > > It's thread safe, you can't extend from it, and no one can > > > > create an instance of it but the Singleton itself. > > > > -c
|
Mon, 16 May 2005 12:54:35 GMT |
|
|