Dear Sirs,
the reason of my question is a problem in my "real" application. But I can't
reproduce it in small examples.
May be you have any ideas why this happens.
In my understanding if I initialize member like this:
private SomeClass _member = new SomeClass();
than compiler will move this initialization into all constructors. (And it
is what compiler does on small examples).
But in "real" applicaton, compiler moves the initialization into one
constructor only (I think default constructor). And if I create an object
with other constructors, then _member is null.
Can somebody help me to figure out, what the reason is?
Thank you in advance,
Boni
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> schrieb im
Newsbeitrag news:90**********************************@microsof t.com...
thanks - yes I did. When you are late and in a rush that is the kind of
thing that happens :-)
"Peter Kirk" wrote:
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> skrev i en
meddelelse news:85**********************************@microsof t.com... > Hi Boni,
> al instance members will be initialized before the constructor is
> executed. For example:
>
> class SomeClass
> {
> public SomeClass()
> {
> Console.WriteLine("SomeClass being created");
> }
> }
>
> class MyClass
> {
> private SomeClass _member = new SomeClass();
>
> public MyClass()
> {
> Console.WriteLine("MyClass being created");
> }
> }
>
> and you do the following:
>
> MyClass x = new MyClass();
>
> you would see in the standard output:
> MyClass being created
> SomeClass beign created
>
> In short all member instances will be initalized before any constructor
> gets
> called.
Don't you mean that the output will be:
SomeClass beign created
MyClass being created