Re: initialising properties in static constructor

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 10/21/04


Date: Thu, 21 Oct 2004 10:48:30 -0400

Hi Mike,

When static constructor is static it cannot initialize instance variables
simply because it has no relations to any instances. Thus you cannot use it
for this purpose unless the fields are not static as well.

You have options:

1. To intialize them in the constructor. If you have more then one
constructor, though, it might be hard to maintain such a code. In this case
initialize them at the moment of their decalration

class Foo
{
    Bar b = new Bar()

    public Foo(...)
    {
    }
}

The code for such initialization will be populated by the compiler at the
begining of all constructrs your calass may have. The drawback that some of
the variables will be initialized twise, which depending on the type of the
obejct might be no acceptable. The other limitation is that you cannot use
*this* which in the constructor is allowed.

2. You can initialize the backup fileds of the properties on demand. The
first time the propety is read if the value is not initialized then you
initialize it. This however works for reference types, but not for value
types unless you don't keep some flags and stuff.

public Foo MyProp
{
    get
    {
        if(this.myProp == null)
        {
            this.myProp = new Foo();
        }
        return myProp;
    }
}

3. you can throw an exception of the roperty is not initialized. Again it
worgs well for reference types, but not for value types unless you don have
special flags

-- 
HTH
Stoitcho Goutsev (100) [C# MVP]
"Mike P" <mrp@telcoelectronics.co.uk> wrote in message 
news:enwQxs2tEHA.1276@TK2MSFTNGP12.phx.gbl...
>I want to use default values...I just wondered whether I should be
> setting them in the static constructor or my other constructors.  I
> guess the answer is to set the defaults in the other constructors.
>
>
> Thanks,
>
> Mike
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it! 


Relevant Pages

  • Re: Inherited Methods and such
    ... what I see in Initialize is ... Now, what you want is to get in the constructor of some S derived from T, ... The base types and registry are part of the framework and the user ... provides the concrete factories. ...
    (comp.lang.ada)
  • Re: Strange member pointer error - even the debugger is confused.
    ... Initialize m_pHead in your constructor to NULL. ... "Aaron Lawrence" wrote in message ... > class ATL_NO_VTABLE CIfsf: ...
    (microsoft.public.vc.language)
  • Re: Initialization & Inheritance
    ... for a recent project I was trying to initialize members of a derived ... class from the constructor of the super class (done using an abstract ... initialize, but double initialization. ... if you used the pointer as it stood, ...
    (comp.lang.java.programmer)
  • Re: Initializing static readonly methods
    ... public class TestClass ... //So assigning a value to readonly is perfectly fine. ... //readonly values are has to be initialized in constructor. ... There is no other place to initialize them. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: static constructor is not guaranteed to be finished?
    ... If you're getting an IO exception when you initialize your log writer ... I have the webservice run; then I continue to work on the code. ... the CLR guarantees to start running the static constructor ...
    (microsoft.public.dotnet.languages.csharp)