Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

common Singleton Pattern implementation using Generic

0.00/5 (No votes)
5 Oct 2011CPOL 11K  
All classes are naturally Lazy loaded.So a better implementation will be:public class Singletonwhere T: new(){ public static readonly T Instance = new T();}You will notice that before calling the singleton class, the object will not be loaded.Surely there are...
All classes are naturally Lazy loaded.
So a better implementation will be:

C#
public class Singleton<T>
where T: new()
{
  public static readonly T Instance = new T();
}


You will notice that before calling the singleton class, the object will not be loaded.
Surely there are differences (as you may want to get the type without instantiating its inner items), but for a general purpose, it works.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)