By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

A quick is this safe singleton question

P: n/a
I usually do singleton implementatios like this:

static readonly myClass instance = new myClass ();

public static myClass Instance
{
get { return instance; }
}

static myClass(){}

but in this situation my constructor has variables and making a static
constructor will not allow for these vars. So i am doing it like this:

static readonly myClass instance = new myClass ();

public static myClass Instance
{
get { return instance; }
}

private myClass ()
{
_someObj = new SomeClass();
_someObj .Completed += OnComplete;
}

I see no problem myself, as the statc instance is created, triggers the
constructor, initliaises the static instance and then i always ref the
static instance. So surely a static constructor is actually unneeded? I just
want to verify i am right in my assumption and there is not something abt a
signleton that i am unaware of.

Thanks
Feb 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I see no problem myself, as the statc instance is created, triggers the
constructor, initliaises the static instance and then i always ref the static
instance. So surely a static constructor is actually unneeded? I just want to
verify i am right in my assumption and there is not something abt a signleton
that i am unaware of.

Thanks
see
http://www.yoda.arachsys.com/csharp/...fieldinit.html

and
http://www.yoda.arachsys.com/csharp/singleton.html
Hans Kesting
Feb 2 '07 #2

P: n/a
SO should i do this?:

static readonly myClass instance = new myClass ();

public static myClass Instance
{
get { return instance; }
}

myClass ()
{
_someObj = new SomeClass();
_someObj .Completed += OnComplete;
}

static myClass ()
{

}
Would that still call the code in my non static contructor?

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
>I see no problem myself, as the statc instance is created, triggers the
constructor, initliaises the static instance and then i always ref the
static instance. So surely a static constructor is actually unneeded? I
just want to verify i am right in my assumption and there is not
something abt a signleton that i am unaware of.

Thanks

see
http://www.yoda.arachsys.com/csharp/...fieldinit.html

and
http://www.yoda.arachsys.com/csharp/singleton.html
Hans Kesting


Feb 2 '07 #3

P: n/a
The non static constructor is used to create an instance of the object. It
doesnt matter how you hold the instance, in a static or non static variable,
it is still an instance and is constructed by an instance constructor.

The static constructor is used to construct the type rather than an instance
of the type. The initialisation of the static field will be compiled into the
static constructor so you code will be

static readonly myClass instance;

public static myClass Instance
{
get { return instance; }
}

myClass ()
{
_someObj = new SomeClass();
_someObj .Completed += OnComplete;
}

static myClass ()
{
instance = new myClass ();
}
when compiled. The static constructor runs once per add domain the first
time a line is encountered that references the type in any way.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"PokerMan" wrote:
SO should i do this?:

static readonly myClass instance = new myClass ();

public static myClass Instance
{
get { return instance; }
}

myClass ()
{
_someObj = new SomeClass();
_someObj .Completed += OnComplete;
}

static myClass ()
{

}
Would that still call the code in my non static contructor?

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
I see no problem myself, as the statc instance is created, triggers the
constructor, initliaises the static instance and then i always ref the
static instance. So surely a static constructor is actually unneeded? I
just want to verify i am right in my assumption and there is not
something abt a signleton that i am unaware of.

Thanks
see
http://www.yoda.arachsys.com/csharp/...fieldinit.html

and
http://www.yoda.arachsys.com/csharp/singleton.html
Hans Kesting


Feb 2 '07 #4

P: n/a
PokerMan <no****@pokercat.co.ukwrote:
SO should i do this?:

static readonly myClass instance = new myClass ();

public static myClass Instance
{
get { return instance; }
}

myClass ()
{
_someObj = new SomeClass();
_someObj .Completed += OnComplete;
}

static myClass ()
{

}
Would that still call the code in my non static contructor?
Yes - the only difference is how "lazy" it will be. If you don't really
mind if the instance is constructed a bit early, or in some situations
where you don't end up needing it, you can do without the static
constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.