Solved

C# .Net

Posted on 2010-08-30
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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