Solved

C# .Net

Posted on 2010-08-30
4
208 Views
Last Modified: 2013-12-17
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
Comment
Question by:vivekj2004
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33558333
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
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33558396
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
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33566399
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
 
LVL 8

Expert Comment

by:Volox
ID: 33573928
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now