Possible Duplicate:
Difference between static class and singleton pattern?
we use static class for common operation. the same thing can be done singleton class also
here i am giving two class one is static class and one is singleton class. actually things is getting harder for me that when one should go for static class and when we should go for singleton class.
public sealed class SiteStructure
{
/// <summary>
/// This is an expensive resource we need to only store in one place.
/// </summary>
object[] _data = new object[10];
/// <summary>
/// Allocate ourselves. We have a private constructor, so no one else can.
/// </summary>
static readonly SiteStructure _instance = new SiteStructure();
/// <summary>
/// Access SiteStructure.Instance to get the singleton object.
/// Then call methods on that instance.
/// </summary>
public static SiteStructure Instance
{
get { return _instance; }
}
/// <summary>
/// This is a private constructor, meaning no outsiders have access.
/// </summary>
private SiteStructure()
{
// Initialize members, etc. here.
}
}
static public class SiteStatic
{
/// <summary>
/// The data must be a static member in this example.
/// </summary>
static object[] _data = new object[10];
/// <summary>
/// C# doesn't define when this constructor is run, but it will likely
/// be run right before it is used.
/// </summary>
static SiteStatic()
{
// Initialize all of our static members.
}
}
please explain when one need to create static class and when singleton class. thanks