Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

when to use static class or Singleton class

$
0
0

here is two piece of class code one is for Singleton and other one is for static class. i like to understand in programming world 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.

i am not looking for difference between singleton & static class.....rather like to know when i use singleton or static class?

publicsealedclassSingleton{privatestaticSingleton instance =null;privatestaticreadonlyobject padlock =newobject();Singleton(){}publicstaticSingletonInstance{
        get{lock(padlock){if(instance ==null){
                    instance =newSingleton();}return instance;}}}}publicstaticclassTestStatic//: ITestSingleton{publicstaticvoid doAction(string args){Console.WriteLine("Test Static :: "+ args);}}
thanks

Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>