Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

C# .Net

Can someone answer the following questions from the following code:
1. singleton can be implemented by marking constructor as private. Why are they using static constructor and another private constructor?
2. why are there 2 constructors in one class? (static and private X(); Is it legal?)
3. Is there a purpose of this design in the following code?

//code begining**********************************

public class X
{
 private static readonly X instance = new X();
 static X()
        {
        }
        private X()
        {
            // sets the default BPM Service Name:
            this.launchData.Add( Launch.Util.Data.CM_SERVICE_NAME,
                ConfigurationManager.AppSettings[ "bpm.service.name" ] );
        }
        internal static X Instance
        {
            get
            {
                return instance;
            }
        }                

end of code**********************************************************
0
vivekj2004
Asked:
vivekj2004
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
the private constrcutor is designed to prevent from instantiate the X class in the regular way:X my_x = new X(); -> invoke default constructor, but if it's private then error in compilation
0
 
Meir RivkinFull stack Software EngineerCommented:
the static constructor is called once in the first time the Instance property of the X class is called, but if has no implementation it can be dropped.so to give the static ctor a meaning you could have type:    public class X    {        private static readonly X instance = null; -> initialize to null        static X()        {            instance = new X(); ->initialize the singleton in the static ctor        }        private X()        {            // sets the default BPM Service Name:         }        internal static X Instance        {            get            {                return instance;            }        }    }another implementation (late instatiation) of singleton could be:public class X    {        private static X instance = null;        private X()        {            // sets the default BPM Service Name:         }        internal static X Instance        {            get            {                if (instance == null)                {                    instance = new X();                }                return instance;            }        }    }both examples are not thread-safe by any means.i recommend this singleton tutorial: http://www.yoda.arachsys.com/csharp/singleton.html
0
 
Gururaj BadamCommented:
Design Patterns only talk about common Problem and how they can be approached. The way they can be implemented can vary from Person to Person. There's no harm how you code it unless the purpose is achieved.

Here's a reference to Patterns Factory from GOF (Gang of Four)

http://www.dofactory.com/Patterns/PatternSingleton.aspx
0
 
VoloxCommented:
Often times the static constructor is used to actually instantiate the singleton so as to avoid thread safety issues.  More info here: http://www.yoda.arachsys.com/csharp/singleton.html 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now