here is two piece of class code one is for Singleton and other one is for static class. i like to understand in programming when one should use static class and when one should use Singleton class?
both are used to hold the global object as a result we can access those data from any where of the program when it is running. scope is broad for both....the life time of application.
1) i really do not find any article which can guide me when i should use static class and when Singleton class should be good choice. i have seen people manage db connection using Singleton class.
2) what is the main difference between Singleton class & static class ?
public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
}
public static class TestStatic //: ITestSingleton
{
public static void doAction(string args)
{
Console.WriteLine("Test Static :: " + args);
}
}