Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# .Net

Posted on 2010-08-30
4
Medium Priority
?
215 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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