I don't approve of public fields either. My point was, the best way to
implement the singleton pattern is to use a static field, optionally with
a static constructor. This is automatically thread-safe, and the object
will not be constructed until the class is used. No need for locking, etc.
Here is how I do it:

sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    public static Singleton Instance
    {
        get { return instance; }
    }
    private Singleton() {}
}

It really is that easy in C#.


On Sat, 8 Jun 2002 08:49:30 -0700, Stefan Holdermans
<[EMAIL PROTECTED]> wrote:
>The main issue is: I really feel bad about public fields, whether they are
>static or non-static... I think that we've been using accessor methods
>and -- now -- properties for all those reasons. So why would we throw away
>that now?

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to