Welcome, Guest

Singleton vs Static class

Views: 356 | Like0 | Comments: 0
A class is said to be static if it solely has static methods, which more accurately should be referred to as functions. A totally procedural design philosophy is shown in the form of a static class.

On the other side, singleton is a design pattern that is exclusive to object-oriented programming. It is an instance of an object with all of the possibilities that are inherent in it, such as polymorphism, and it has a creation mechanism that assures that there will only ever be a single instance of that specific role over the whole of its lifespan.
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
   
namespace SingletonVsStaticClass  
{  
    class SomeClass  
    {  
        public int ABC;  
    }  
   
    public sealed class SingletonSampleClass  
    {  
        static  SingletonSampleClass _instance;  
   
        public static SingletonSampleClass Instance  
        {  
            get { return _instance ?? (_instance = new SingletonSampleClass()); }  
        }  
        private SingletonSampleClass() { }  
   
        public string Message { get; set; }  
    }  
   
    static public class StaticSampleClass  
    {  
        private static readonly int SomeVariable;  
        //Static constructor is executed only once when the type is first used.   
        //All classes can have static constructors, not just static classes.  
        static StaticSampleClass()  
        {  
            SomeVariable = 1;  
            //Do the required things  
        }  
        public static string ShowValue()  
        {  
            return string.Format("The value of someVariable is {0}", SomeVariable);  
        }  
        public static string Message { get; set; }  
    }  
    class Program  
    {  
   
        static void Main(string[] args)  
        {  
            //Normal class instantiation and usage  
            var someClass = new SomeClass {ABC = 5};  
            Console.WriteLine("Normal class usage: "+someClass.ABC);  
   
            //Static Class instantiation  
            string returnValue = StaticSampleClass.ShowValue();  
            Console.WriteLine("Static class usage: " + returnValue);  
   
            //Singleton class instantiation  
            var singletonSampleClass = SingletonSampleClass.Instance;  
            singletonSampleClass.Message = "Hello";  
            Console.WriteLine("Singleton class usage: " + singletonSampleClass.Message);  
              
            //Test the instances if are equal  
            var anotherSingletonSampleClass = SingletonSampleClass.Instance;  
            Console.WriteLine("Checking if both instances are same: "+ singletonSampleClass.Equals(anotherSingletonSampleClass) );  
   
            Console.Read();  
        }  
    }  
}  

You describe it, I build it

Great software is not cheap to make. Never use bad software.
MR Quick Services
Merge PDF Files
HTML to PDF
PrivacyAboutContact