yqm的.net之园

writing down what i am experiencing or creating
没有使用lock的Singleton
public sealed class Singleton
{
    Singleton()
    {
    }

    
public static Singleton Instance
    {
        
get
        {
            
return Nested.instance;
        }
    }
    
    
class Nested
    {
        
static Nested()
        {
        }

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

 

 

泛型实现

 1public class Singleton<T> where T : new()
 2{
 3   public static T Instance
 4   {
 5      get return SingletonCreator.instance; }
 6   }

 7
 8   class SingletonCreator
 9   {
10       internal static readonly T instance = new T();
11   }

12}


 参考文章:http://www.yoda.arachsys.com/csharp/singleton.html

posted on 2008-09-10 11:29  YQM  阅读(168)  评论(0编辑  收藏  举报