The Singleton User



  • I was dumbfounded by this...  So let me lay this one out so that you can appreciate this WTF in it's entirety.

    In a legacy ASP.NET app I'm rewriting, there is some logic in the page load to enforce security, like such:

    [code]SecurityObj sObj = new SecurityObj();[/code]

    [code]if (sObj.IsUserAdmin) { // do admin stuff } [/code]

    Ok, kinda wierd... How does the security object know if the user is admin? Oh, probably it checks the current principal, or even possibly a data slot in the current thread (I've seen it before).  So I F12 the IsAdmin property, and this is what I see:

    [code]public bool IsAdmin {get {return GlobalConstants.CurrentUserAccount.IsAdmin; } }[/code]

    WTF?!  Ok, lets see the definition of CurrentUserAccount:

    [code]public static UserObject CurrentUserAccount = new UserObject();[/code]

    So the dev unwittingly created a globally accessed singleton user object which is good for the life of the application, for an obviously multi-threaded web application accessed by several different groups of users...  I'll let you ponder the potential implications.

    Oh, and a minor WTF which just shows the extent of the lack of understanding, the GlobalConstants class is an instance object containing nothing but static and constant fields.

    I guess this explains some of the wierd bugs.

    EDIT: I just noticed this now (notice the double call for no real reason):

    [code]if (dao.GetData().Count > 0) {[/code]

    [code]grid.DataSource = dao.GetData();[/code]

    [code]}[/code]



  •  That annoys me even more than people afraid to put genuinely common, expensive data in Application scope.



  • Not to say this isn't a WTF, but this also is not a singleton.  It is simply an object created and placed in global scope.

    A singleton garuntees there will only ever be one copy created, regardless of variable name used, this does not.



  • @KattMan said:

    Not to say this isn't a WTF, but this also is not a singleton.  It is simply an object created and placed in global scope.

    A singleton garuntees there will only ever be one copy created, regardless of variable name used, this does not.

    You sure about that?  It's static, and that makes it a singleton.  The CLR guarantees it.


  •  @KattMan said:

    Not to say this isn't a WTF, but this also is not a singleton.  It is simply an object created and placed in global scope.

    A singleton garuntees there will only ever be one copy created, regardless of variable name used, this does not.

    Agreed.

     



  • It would only be a singleton if UserObject's constructor were private or internal.  Static != Singleton.

    Here is the singleton pattern:

    public class Singleton

    {

    private static Singleton _instance = new Singleton();

    private Singleton() {}

    public static Singleton GetInstance() { return _instance; }

    }



  • Ok, take a look at this and tell me what you see:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>public</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>class</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass

    </FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> MyClass() { }</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>public</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>static</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> Instance = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>();

    }

    </FONT></FONT>


  • I agree, static != singleton, but the CLR makes it thread safe, and static ctors only get called once (even if an exception is thrown in the ctor).

    EDIT: my bad, you're right, it's not a singleton...  Long night and only one coffee so far today.

    Either way, it's still a huge WTF that users can potentially share the same user object because it is static and globally accessed. 



  • @C-Octothorpe said:

    Either way, it's still a huge WTF that users can potentially share the same user object because it is static and globally accessed. 
    Indeed!  Which leaves just one question: where can I get a pound of the weed this idiot was smoking?



  • Apparently I've been smoking it...

    Something I forgot in my example was the readonly keyword... That's why it's a singleton.

    public static readonly object Instance = new object();

    Done...



  • Being ASP.NET, that static value is NOT going to be shared among different users. It's not even going to keep it's value between two page loads, since for evey page load it's going to be instantiated again.



  • Sure it is...  Try it yourself explicitly with a static ctor (which is what it does under the sheets anyway.  You'll see it's only called once for the lifetime of the application.

    The fact that it's asp.net really has nothing to do with it.  Imagine this in a service class that's being called from a WCF service.  I just thought it was WTFy because it's being used as a session object, which is per user, and not as a global object.

    You can achieve caching this way too.



  • @C-Octothorpe said:

    Apparently I've been smoking it...

    Something I forgot in my example was the readonly keyword... That's why it's a singleton.

    public static readonly object Instance = new object();

    Done...

    Yep, you have a singleton there, looking at the prior code for this class, which wasn't in the original post, you have a class, with a private constructor, that creates an instance of itself for cosumption.

    Now have this created on app startup and accessed everywhere from there, then everyopne will have access rights based off of whoever logged in first and caused the app to start.

    If it is not saved there, then it is created every time, potentially with null values.

    Either way, this is a WTF.

     



  • @C-Octothorpe said:

    Sure it is...  Try it yourself explicitly with a static ctor (which is what it does under the sheets anyway.  You'll see it's only called once for the lifetime of the application.

    The fact that it's asp.net really has nothing to do with it.  Imagine this in a service class that's being called from a WCF service.  I just thought it was WTFy because it's being used as a session object, which is per user, and not as a global object.

    You can achieve caching this way too.

    If this is saved in the session, then I don't see the problem.  Recreated only on session startup within the session isolated from other sessions, it will retian the rights for the user logged into that session from one page request to another.



  • That's the thing, it's not in session, but it's being used as a session just because of it's global scope.

    Also, even if this wasn't readonly (singleton), it still introduces a race condition as there is only one value being shared for all the threads.  One thread comes in, sets the correct static value, another thread comes in, overrides that static value with it's own, and now both threads are running as the second user.



  •  @C-Octothorpe said:

    Sure it is...  Try it yourself explicitly with a static ctor
    I never thought of it that way, but you're right!  That makes caching even easier by using Singleton pattern

     Thanks dude!



  • One man's race condition is another man's optimistic locking.



  • So I checked it and you guys are right. Static in ASP.NET is practically global. And I'm thinking about the possibilities too.



  • @hoodaticus said:

    @C-Octothorpe said:
    Sure it is... Try it yourself explicitly with a static ctor
    I never thought of it that way, but you're right! That makes caching even easier by using Singleton pattern

    Thanks dude!

    Out of curiosity, how did you think the System.Web.Caching.Cache class worked?



  • @hoodaticus said:

    I never thought of it that way, but you're right!  That makes caching even easier by using Singleton pattern

    Thanks dude!

    NP.  I always called it the poor mans cache (vs. ent-lib cache block, etc.).  You obviously have to take on thread-safety (just double if-lock the bitch and you're golden), but it can be very awsome.  Dangerous in the wrong hands though.  One contract I worked at they had a static dictionary being populated once, but didn't lock on it, followed by a swallowed exception.  And it was the dev lead that wrote it.  Hilarity ensued...

    @hoodaticus said:

    One man's race condition is another man's optimistic locking.

    HA!



  • @blakeyrat said:

    @hoodaticus said:
    @C-Octothorpe said:
    Sure it is... Try it yourself explicitly with a static ctor
    I never thought of it that way, but you're right! That makes caching even easier by using Singleton pattern

     

    Thanks dude!

    Out of curiosity, how did you think the System.Web.Caching.Cache class worked?
    Does anybody still use that?  Seriously, not trolling...


  • @C-Octothorpe said:

    Does anybody still use that? Seriously, not trolling...

    Why, is it deprecated or something?

    I have an app that uses it all over the place to avoid performing database queries (including Amazon SimpleDB, which is relatively slow) for stuff it already knows. The only "downside" is that if I make a change to one of the DB tables, it takes up to an hour for the app to refresh its cache. But that doesn't matter for my use case, so.



  • @C-Octothorpe said:

    Ok, take a look at this and tell me what you see:

    <FONT face=Consolas size=2><FONT face=Consolas size=2>

    </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>public</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>class</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2>MyClass

    </FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2>

    {

    </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>private</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> MyClass() { }</FONT></FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>public</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>static</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2>MyClass</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> Instance = </FONT></FONT><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2><FONT face=Consolas color=#0000ff size=2>new</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2> </FONT></FONT><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2><FONT face=Consolas color=#2b91af size=2>MyClass</FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2>();</FONT></FONT></FONT></FONT><FONT face=Consolas size=2><FONT face=Consolas size=2>

    }

    </FONT></FONT>

    Interesting.  I'm pretty sure you could post an article on Codeproject about this.  Call it "Singleton Simplification Using Eager Initialization Through Implicit Static Construction".



  • @blakeyrat said:

    @C-Octothorpe said:
    Does anybody still use that? Seriously, not trolling...
    Why, is it deprecated or something?

    I have an app that uses it all over the place to avoid performing database queries (including Amazon SimpleDB, which is relatively slow) for stuff it already knows. The only "downside" is that if I make a change to one of the DB tables, it takes up to an hour for the app to refresh its cache. But that doesn't matter for my use case, so.

    No worries...  I was just wondering if anybody still used that particular object as recently I have tended more to bury my caching mechanisms beyond the UI layer, like service, or DB, where you don't have a reference to system.web.  But I definitely see the value in having a nice single point of entry for all my caching needs, including cache expiration, etc.


  • @frits said:

    Call it "Singleton Simplification Using Eager Initialization Through Implicit Static Construction".

    That is epic.



  • @C-Octothorpe said:

    You obviously have to take on thread-safety
    I already multithread everything, so I thread-safety my classes out of habit.  So what happened - key not found exceptions when the thing was populating and getting read at the same time?

    That [u]would[/u] be embarassing.  Especially since using a temporary collection to hold your updated data and then swapping it out for the old collection is generally thread safe and non-locking.



  • @hoodaticus said:

    @C-Octothorpe said:

    You obviously have to take on thread-safety
    I already multithread everything, so I thread-safety my classes out of habit.  So what happened - key not found exceptions when the thing was populating and getting read at the same time?

    That would be embarassing.  Especially since using a temporary collection to hold your updated data and then swapping it out for the old collection is generally thread safe and non-locking.

    I forget *exactly* how it happened, but it was something along the lines of check for null, then fill the dictionary (beautiful race condition here as at least 3 or 4, or more threads, depending on load, could enter this, what should be critical section), which would throw a dupe key exception, which was swallowed, but then further threads would only have access to a partially populated dictionary because it was only ever checking for null.

    I still remember the "what's thread-safe" look on his face as I asked him what he though would happen if two concurrent threads tried to populate the dictionary, and why he thought his code would prevent this when he wrote it.



  • @blakeyrat said:

    Why, is it deprecated or something?
    I use business objects for everything I do.  No need for webcache when you've got an asynchronously self-updating thread safe domain model



  • @C-Octothorpe said:

    I still remember the "what's thread-safe" look on his face
    Priceless!



  • @hoodaticus said:

    @blakeyrat said:

    Why, is it deprecated or something?
    I use business objects for everything I do.  No need for webcache when you've got an asynchronously self-updating thread safe domain model

    Are you talking about the SAP product?



  • @blakeyrat said:

    Are you talking about the SAP product?
    Hell no.  Just classes I write.  I call them "business objects" because they encapsulate data, behavior, and data access.  The M in MVC.

    Since my model/business objects are self-updating, no need for the webcache.



  • @hoodaticus said:

    @blakeyrat said:
    Are you talking about the SAP product?
    Hell no. Just classes I write. I call them "business objects" because they encapsulate data, behavior, and data access. The M in MVC.

    Since my model/business objects are self-updating, no need for the webcache.

    Ah. Well, the app I maintain that uses the Cache class pretty much does nothing except validate a few dozen URL params, combine them with a few other bits of data, and shove them into a database. That would be overkill for it. It's designed to be scalable, so it's stateless-- most of the caching is of the form "I've already seen visitor# 55443 so don't bother putting that visitor into the DB again".



  • @KattMan said:

    @C-Octothorpe said:

    Apparently I've been smoking it...

    Something I forgot in my example was the readonly keyword... That's why it's a singleton.

    public static readonly object Instance = new object();

    Done...

    Yep, you have a singleton there, looking at the prior code for this class, which wasn't in the original post, you have a class, with a private constructor, that creates an instance of itself for cosumption.

    Not to be a complete asshole or anything, but that is not a singleton.  A singleton is a class that can have no more than one instance in a single application.  Every object in the system is of type object, so that is literally as far from true as you can possibly get.

     



  • @hoodaticus said:

    Not to be a complete asshole or anything, but that is not a singleton.  A singleton is a class that can have no more than one instance in a single application.  Every object in the system is of type object, so that is literally as far from true as you can possibly get.

    You are actually being 100% correct <grin>.  If there is a potentially valid code path (regardless of if it exists or not in the actual code) that can invoke the constructor more than once, then it is NOT a singleton. The static instance in the example is simply that. There is nothing in the code (at least as presented) that prevents other people from writing code that also creates instances of the specific type.

    If C-Octothorpe's example was a Singleton (again it is not), then it would be impossible (or at least illegal to the point where the second and subsequent calls should fail) ) to create any instances of "object" once the first instance was created.

     CLARIFICATION: When talking "possible" I am neglecting "subterfuge" means such as reflection, and focusing solely "normal" programming techniques.



  • Sorry, I disagree with both of you...  Take this example:

    <FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    public </FONT></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>class</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass

    </FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> MyClass() { }</FONT></FONT>

    </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>public</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>static</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>readonly</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> Instance = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    </FONT></FONT>Now obviously the ctor can be invoked from somewhere else within the class, or even via reflection which would create more than one instance within a process (running application).  Even if you used the "classic" singleton pattern (property exposing "instance" which performs the double-if lock via getter, etc.), it can still be subverted as there is nothing in the CLR that will stop that from happening.  I like this because the locking and thread safety is handled by the CLR (even though I *do* know how to implement the classic singleton patter).

    Other than raw implementation, whats the difference between what you see above and what you see below:

    <FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    public </FONT></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>class</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass2

    </FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> MyClass2() { }</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>static</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>readonly</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>object</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> _lock = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>object</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>static</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass2</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> _instance = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;</FONT></FONT>

    </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>public</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass2</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> Instance</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>get</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (_instance == </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>lock</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (_lock)</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    {

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (_instance == </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)

    _instance = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyClass2</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    }

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>return</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> _instance;</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    }

    }

    }

    </FONT></FONT>


  •  That is a singleton.  The code I quoted was not, since Instance was of type object.



  • @hoodaticus said:

    That is a singleton.  The quote I quoted was not, since Instance was of type object.
    Sorry, I just used object because I wanted to show that I missed the realonly keyword.  My bad.



  •  I know you know what a singleton is and always have - I was clarifying for the unwashed masses.



  • Gotcha...


Log in to reply