Lazy thread safe singleton for .NET 3.5 or older alternate implementation

suggest change

Because in .NET 3.5 and older you don’t have Lazy<T> class you use the following pattern:

public class Singleton
{
    private Singleton() // prevents public instantiation
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

This is inspired from Jon Skeet’s blog post.

Because the Nested class is nested and private the instantiation of the singleton instance will not be triggered by accessing other members of the Sigleton class (such as a public readonly property, for example).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Singleton Implementation:
* Lazy thread safe singleton for .NET 3.5 or older alternate implementation

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
42 Singleton Implementation
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script