473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Singleton implementation

Hey guys

I implement my singleton by doin this at the top of the class.

public static readonly DrawDetails Instance = new DrawDetails();

Then to access the instance i do DrawDetails.Instance.SomeMethod();

My query is, everytime i call that line does it call the constructor again?
Or is the constructor called once, the first time i use it and from then on
it just reuses it without calling the constructor.

I presume it doesnt keep calling it as i made it static but i just want to
be sure.

Thanks.
May 17 '06 #1
4 1306
Daniel,

The constructor is called just once, the first time the type is
referenced.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel" <Da*****@vestryonline.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hey guys

I implement my singleton by doin this at the top of the class.

public static readonly DrawDetails Instance = new DrawDetails();

Then to access the instance i do DrawDetails.Instance.SomeMethod();

My query is, everytime i call that line does it call the constructor
again? Or is the constructor called once, the first time i use it and from
then on it just reuses it without calling the constructor.

I presume it doesnt keep calling it as i made it static but i just want to
be sure.

Thanks.

May 17 '06 #2

For a singleton, you hide/private the default constructor.

There should be one static method (usually people call it .GetInstance() )
to return your object.

I have a singleton example at:

http://spaces.msn.com/sholliday/ 12/1/2005 entry

Download the code (near the bottom of the entry)
and look at the code in InMemoryObjectHolder.cs
...
"Daniel" <Da*****@vestryonline.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hey guys

I implement my singleton by doin this at the top of the class.

public static readonly DrawDetails Instance = new DrawDetails();

Then to access the instance i do DrawDetails.Instance.SomeMethod();

My query is, everytime i call that line does it call the constructor again? Or is the constructor called once, the first time i use it and from then on it just reuses it without calling the constructor.

I presume it doesnt keep calling it as i made it static but i just want to
be sure.

Thanks.

May 17 '06 #3
Hello Daniel,

Take into account that pure .net singleton is not an easy task, because CLR
create a copy of static members for each application
domain

You realization is just an application singleton

D> Hey guys
D> I implement my singleton by doin this at the top of the class.
D> public static readonly DrawDetails Instance = new DrawDetails();
D> Then to access the instance i do DrawDetails.Instance.SomeMethod();
D> My query is, everytime i call that line does it call the constructor
D> again? Or is the constructor called once, the first time i use it and
D> from then on it just reuses it without calling the constructor.
D> I presume it doesnt keep calling it as i made it static but i just
D> want to be sure.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 17 '06 #4
sloan <sl***@ipass.net> wrote:
For a singleton, you hide/private the default constructor.

There should be one static method (usually people call it .GetInstance() )
to return your object.

I have a singleton example at:

http://spaces.msn.com/sholliday/ 12/1/2005 entry

Download the code (near the bottom of the entry)
and look at the code in InMemoryObjectHolder.cs


It's not thread-safe, unfortunately.

See http://www.pobox.com/~skeet/csharp/singleton.html

By the way, do you realise that the

if (constant == variable)

idiom used in C/C++ to avoid accidental assignment instead of
comparison is unnecessary in C#? I believe most people find

if (variable == constant)

easier to read (I know I do).

--
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
May 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
10
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
3
by: Raider | last post by:
I need to have one object for each template argument(s) used. For example, I need to have one object of int. I tried the following code and it gives me all I want with Visual C++ 7.1. But is it...
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: sam_cit | last post by:
Hi everyone, I have the following code and it gives a linker error on MS vc++ 6.0. error LNK2001: unresolved external symbol "protected: __thiscall Singleton::Singleton(void)"...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Matthew Fitzgibbons | last post by:
Urizev wrote: I've run into a similar problem. Basically, when you refer to a class from __main__ (i.e., the .py file you're actually running) versus from an imported module, Python doesn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.