Click here to Skip to main content
15,903,175 members
Home / Discussions / C#
   

C#

 
GeneralRe: About Hashtables Pin
Harvey Saayman1-Apr-08 22:26
Harvey Saayman1-Apr-08 22:26 
QuestionHow to perform cut,delete operation in bitmap? Pin
Aravinthan1-Apr-08 18:51
Aravinthan1-Apr-08 18:51 
GeneralConfused with the Equals() of Object class [modified] Pin
Darmi1-Apr-08 17:48
Darmi1-Apr-08 17:48 
GeneralRe: Confused with the Equals() of Object class Pin
N a v a n e e t h1-Apr-08 20:38
N a v a n e e t h1-Apr-08 20:38 
GeneralRe: Confused with the Equals() of Object class Pin
carbon_golem2-Apr-08 2:48
carbon_golem2-Apr-08 2:48 
GeneralSite Personalization - UserID from the asp_net Profile table Pin
karmasol1-Apr-08 17:46
karmasol1-Apr-08 17:46 
QuestionProblem with Singleton Pattern Pin
Zero Destiny1-Apr-08 17:39
professionalZero Destiny1-Apr-08 17:39 
GeneralRe: Problem with Singleton Pattern Pin
Simon P Stevens1-Apr-08 22:25
Simon P Stevens1-Apr-08 22:25 
I was interested by this one so I thought I'd try it out. My full code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SomeClass theClass = SomeClass.Current;
            theClass.RunMethod();

            Console.ReadLine();
        }
    }

    public class SomeClass
    {
        private static SomeClass _current = null;

        public static SomeClass Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new SomeClass();
                }

                return _current;
            }
        }

        public SomeClass()
        {
            Console.WriteLine("Constructor");
        }

        public void RunMethod()
        {
            Console.WriteLine("Method");
        }
    }
}


As you'd expect, when I run this, it prints out both strings. So then I started to debug it to see what was happening. As I debugged, I hovered my mouse over the various variables as I always do to check the state of things, and lo and behold, it skipped straight over the if statement just like you described, and I realised what might be happening.

SomeClass.Current is a static member that creates the instance the first time it is called. That can include being called by any attached debugger trying to look at the static member, so when I hovered my mouse over the SomeClass.Current bit, that action kicked off the constructor, so when I stepped into the routine it had already run.

The really interesting thing about this is that if you stick a break point in the constructor it never gets hit! The Visual studio debugger must disable break points when it runs code for property evaluation.

I suggest you have a look in your watch window and see if you've got a watch on the static member that will be having the effect of calling the constructor. You could also try sticking a Debug.WriteLine in the constructor and check that it does indeed output something.




(Incidentally, you should bear in mind that your singleton isn't thread safe. It might not matter for your use, but you should take a look at this article for a discussion of the singleton pattern in c# http://www.yoda.arachsys.com/csharp/singleton.html[^])

Simon

GeneralRe: Problem with Singleton Pattern Pin
Rob Philpott1-Apr-08 22:49
Rob Philpott1-Apr-08 22:49 
GeneralRe: Problem with Singleton Pattern Pin
Rob Philpott1-Apr-08 22:51
Rob Philpott1-Apr-08 22:51 
GeneralRe: Problem with Singleton Pattern Pin
carbon_golem2-Apr-08 3:03
carbon_golem2-Apr-08 3:03 
GeneralRe: Problem with Singleton Pattern Pin
Zero Destiny2-Apr-08 4:22
professionalZero Destiny2-Apr-08 4:22 
QuestionHow to pass a parameter in an event Pin
Silvyster1-Apr-08 16:08
Silvyster1-Apr-08 16:08 
GeneralRe: How to pass a parameter in an event Pin
PandemoniumPasha1-Apr-08 17:38
PandemoniumPasha1-Apr-08 17:38 
GeneralRe: How to pass a parameter in an event Pin
Silvyster1-Apr-08 22:12
Silvyster1-Apr-08 22:12 
GeneralRe: How to pass a parameter in an event Pin
J4amieC1-Apr-08 22:30
J4amieC1-Apr-08 22:30 
GeneralInfos, links, best practice etc. for programming a webservice Pin
stephan_0071-Apr-08 12:02
stephan_0071-Apr-08 12:02 
GeneralRe: Infos, links, best practice etc. for programming a webservice Pin
KaptinKrunch1-Apr-08 14:05
KaptinKrunch1-Apr-08 14:05 
GeneralRe: Infos, links, best practice etc. for programming a webservice Pin
Rob Philpott1-Apr-08 22:45
Rob Philpott1-Apr-08 22:45 
Question(XML) Editing a single element within a node Pin
Stupefy1-Apr-08 10:13
Stupefy1-Apr-08 10:13 
GeneralRe: (XML) Editing a single element within a node Pin
damianrda1-Apr-08 10:56
damianrda1-Apr-08 10:56 
GeneralRe: (XML) Editing a single element within a node Pin
Stupefy1-Apr-08 11:03
Stupefy1-Apr-08 11:03 
GeneralRe: (XML) Editing a single element within a node Pin
led mike1-Apr-08 11:04
led mike1-Apr-08 11:04 
GeneralRe: (XML) Editing a single element within a node Pin
led mike1-Apr-08 10:59
led mike1-Apr-08 10:59 
QuestionA style question regarding multi-signature methods Pin
Clive D. Pottinger1-Apr-08 9:19
Clive D. Pottinger1-Apr-08 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.