Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

C# 4 new way to write Singletons

Jun 1 2011 8:45AM by Rui Carvalho   

Private static readonly is today the preferred way to create a singleton in C#, but it don't have much laziness. You can implement it with a nested class, but with C#4 you can directly use the System.Lazy<T> class. Here is the model of the implementation:

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}
Read More..   [30 clicks]

Published under: Microsoft .NET Tips ·  ·  ·  · 


Rui Carvalho
44 · 4% · 1326
3
 
0
Knew
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

Your Comment


Sign Up or Login to post a comment.

"C# 4 new way to write Singletons" rated 5 out of 5 by 3 readers
C# 4 new way to write Singletons , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]