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. To Heath (we talked about this subject before)

To Heath (we talked about this subject before)

Scheduled Pinned Locked Moved C#
csharpquestionhtmlcomlearning
6 Posts 3 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.
  • P Offline
    P Offline
    profoundwhispers
    wrote on last edited by
    #1

    public sealed class Singleton { static readonly Singleton instance=new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton() { } Singleton() { } public static Singleton GetInstance() { return instance; } } That was from this[^] article. Maybe you can answer this question: Is there an explicit need there for the private constructor to avoid initialization? If we remove it completely, will .NET supply a default public parameterless constructor? Sammy "A good friend, is like a good book: the inside is better than the cover..."

    T H 2 Replies Last reply
    0
    • P profoundwhispers

      public sealed class Singleton { static readonly Singleton instance=new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton() { } Singleton() { } public static Singleton GetInstance() { return instance; } } That was from this[^] article. Maybe you can answer this question: Is there an explicit need there for the private constructor to avoid initialization? If we remove it completely, will .NET supply a default public parameterless constructor? Sammy "A good friend, is like a good book: the inside is better than the cover..."

      T Offline
      T Offline
      Tom Larsen
      wrote on last edited by
      #2

      The point of making a constructor private/protected is so that someone using the object via the API can't create an instance on their own. This is very handy if you want to make an object with singleton behavior. The only way you get the instance (the only instance) is using GetInstance. Doing it this way, there is no other way create the object. If you leave a construtor publicly exposed then it cirumvents the code you put in place to enforce the singleton behavior.

      P 1 Reply Last reply
      0
      • P profoundwhispers

        public sealed class Singleton { static readonly Singleton instance=new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton() { } Singleton() { } public static Singleton GetInstance() { return instance; } } That was from this[^] article. Maybe you can answer this question: Is there an explicit need there for the private constructor to avoid initialization? If we remove it completely, will .NET supply a default public parameterless constructor? Sammy "A good friend, is like a good book: the inside is better than the cover..."

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        If you remove the private default constructor, yes, the compiler will provide one. Also, this isn't the best singleton pattern to use. The one I posted earlier was. It uses a double-check locking approach which eliminates a couple race conditions that may occur. This has been discussed in the past in this forum so if you want to know more, try searching for "singleton" or something for the C# forum.

        Microsoft MVP, Visual C# My Articles

        P 1 Reply Last reply
        0
        • H Heath Stewart

          If you remove the private default constructor, yes, the compiler will provide one. Also, this isn't the best singleton pattern to use. The one I posted earlier was. It uses a double-check locking approach which eliminates a couple race conditions that may occur. This has been discussed in the past in this forum so if you want to know more, try searching for "singleton" or something for the C# forum.

          Microsoft MVP, Visual C# My Articles

          P Offline
          P Offline
          profoundwhispers
          wrote on last edited by
          #4

          Well, what I'm doing right now is, I'm providing the binary deserialization mechanism in the static constructor, since I only need to load the settings from the file once in the lifetime of the program. Then, I'm providing a Save instance method so that I could save multiple times, which is needed whenever the settings dialog is opened and closed with Ok button. If there is nothing wrong with this in your opinion, then don't post an answer; otherwise, please comment. Thanks! Sammy "A good friend, is like a good book: the inside is better than the cover..."

          H 1 Reply Last reply
          0
          • T Tom Larsen

            The point of making a constructor private/protected is so that someone using the object via the API can't create an instance on their own. This is very handy if you want to make an object with singleton behavior. The only way you get the instance (the only instance) is using GetInstance. Doing it this way, there is no other way create the object. If you leave a construtor publicly exposed then it cirumvents the code you put in place to enforce the singleton behavior.

            P Offline
            P Offline
            profoundwhispers
            wrote on last edited by
            #5

            Thank you that was very clarifying. Sammy "A good friend, is like a good book: the inside is better than the cover..."

            1 Reply Last reply
            0
            • P profoundwhispers

              Well, what I'm doing right now is, I'm providing the binary deserialization mechanism in the static constructor, since I only need to load the settings from the file once in the lifetime of the program. Then, I'm providing a Save instance method so that I could save multiple times, which is needed whenever the settings dialog is opened and closed with Ok button. If there is nothing wrong with this in your opinion, then don't post an answer; otherwise, please comment. Thanks! Sammy "A good friend, is like a good book: the inside is better than the cover..."

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              There's nothing wrong with it, but what I was referring to in the previous post could result in your settings being loaded more than once. Now, this probably wouldn't be a problem, but locking your resources will gaurantee that it wouldn't. I'm not trying to down-play your approach, only present different ideas. It's you're implementation - do what you want. :) Typically in a singleton you provide static methods and properties and hide instance details from other classes. For instance, you could also have your Save method as static and it gets the private Instance property (or uses your GetInstance method) and saves the data. For the caller then, it's as simple as doing MySettings.Save. Again, just some things to think about. Your approach would work fine in most cases (only may become a problem with multiple AppDomains in a single process, which may not apply).

              Microsoft MVP, Visual C# My Articles

              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