singleton 
Author Message
 singleton

Hi,

What is the good way, in C# to make singleton objects ?

Thank you.



Sun, 15 May 2005 00:21:41 GMT  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Singleton not a singleton?

2. ATL Singleton and Singleton Class also from C++

3. new Singleton() ( Can I override the new operator to return a singleton?)

4. Singleton Design Pattern Question

5. Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton

6. Singletons & delete error

7. singleton class please help

8. managed C++ Singleton and usage in VB

9. Why wrapping unmanaged C++ singletons can be dangerous

10. Subclassing Singleton

11. Singleton design approach

12. Proper Singleton Design in C#

 

 
Powered by phpBB® Forum Software