Solved

C# .Net

Posted on 2010-08-30
4
206 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now