Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Singleton class in C#

Singleton class in C#

Scheduled Pinned Locked Moved C#
questioncsharp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shubhabrata Mohanty
    wrote on last edited by
    #1

    What is the difference between Singleton implemented using static GetInstance() method and having a Class with all Public Static methods. Is scenario#2 a Singleton class? Please let me know the differences between these two approaches. 1)----------------- public class SealedClass { private static SealedClass _class = new SealedClass(); private SealedClass() {} public static SealedClass GetInstance { get { return _class; } } } -------------------------------------- 2) public class StaticClass { public static string GetName() { return "Shubho"; } public static string GetAddress() { return "Shubho"; } }

    R 1 Reply Last reply
    0
    • S Shubhabrata Mohanty

      What is the difference between Singleton implemented using static GetInstance() method and having a Class with all Public Static methods. Is scenario#2 a Singleton class? Please let me know the differences between these two approaches. 1)----------------- public class SealedClass { private static SealedClass _class = new SealedClass(); private SealedClass() {} public static SealedClass GetInstance { get { return _class; } } } -------------------------------------- 2) public class StaticClass { public static string GetName() { return "Shubho"; } public static string GetAddress() { return "Shubho"; } }

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Interesting question. According to the GoF, the purpose of a singleton is to "ensure a class has only one instance, and provide a global point of access to it." Your second example has no instance, its just a collection of static methods and hence we couldn't get a reference to it. Microsoft has a best practice with regard to singletons - they say it should be implemented as follows - a thread safe lazy implementation

      public class Singleton
      {
      // private constructor to prevent instantiation
      private Singleton() {}

      private static volatile Singleton \_singleton;
      private static object \_lock = new object();
      
      public static Singleton Value
      {
          get
          {
              if (\_singleton == null)
              {
                  lock(\_lock);
                  {
                      if (\_singleton == null) \_singleton = new Singleton();
                  }
              }
              return \_singleton;
           }
       }
      

      }

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups