Re: A quick is this safe singleton question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



PokerMan <nospam@xxxxxxxxxxxxxx> 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?

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 - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: super.clone() puzzlement
    ... public class MyClass { ... public MyClass(MyClass mine) { ... That is, I want a copy of the MyClass instance where, when I change the object referenced by its value, a copy I've made of the MyClass instance keeps its own value object intact. ... public MyClass(T value, ValueCopier<T> copier) { ...
    (comp.lang.java.programmer)
  • Re: Stupid question about List(Of T)
    ... Dim MyList as New List(Of MyClass) ... dim TempC as new MyClass ... Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty ... you are disposing each MyClass instance after you add it to the list. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: super.clone() puzzlement
    ... public class MyClass { ... public MyClass(MyClass mine) { ... That is, I want a copy of the MyClass instance where, when I change the object referenced by its value, a copy I've made of the MyClass instance keeps its own value object intact. ...
    (comp.lang.java.programmer)
  • Re: super.clone() puzzlement
    ... public class MyClass { ... public MyClass(MyClass mine) { ... That is, I want a copy of the MyClass instance where, when I change the object referenced by its value, a copy I've made of the MyClass instance keeps its own value object intact. ...
    (comp.lang.java.programmer)
  • Re: Class Destroys itself straight away!
    ... I have a Class who's constructor accepts an STL wstring, ... seems to kill itself before it can be used. ... MyClass mc = MyClass; ...
    (microsoft.public.vc.language)