Jon-
Your advice helped me greatly. After trying to write a small app that completely showed the problem, I determined that the code
works fine, as long as it isn't running in the debugger.
Here is a small application that exhibits the problem. Note that I'm running in the VS.NET 2003 IDE debugger. When the breakpoint
is turned off, you can see that only one line is output to the console saying that the property has been accessed. When you
breakpoint the designated line in Main(), you can see that the property *appears* to have been called multiple times.
<code>
using System;
namespace StaticInitProblem
{
//StaticInit shows the debugger executing code
//differently when a breakpoint is active.
//To show the problem, set a breakpoint where
//indicated in Main().
public class StaticInit
{
public StaticInit()
{
}
public static int MyProperty
{
get
{
Console.WriteLine("Property accessed.");
return 1;
}
}
}
class StaticInitProblem
{
[STAThread]
static void Main(string[] args)
{
//First, run this app with no
//breakpoints and view the output.
//To show the problem:
//1) Breakpoint the next line of code.
//2) Run the application until it hits the breakpoint.
//3) Debug->Continue execution.
//4) Look at the application output window.
int i = StaticInit.MyProperty;
Console.WriteLine("Press enter to bail.");
Console.Read();
}
}
}
</code>
The interesting side effect I saw was due to the fact that even though the debugger appears to execute the property code multiple
times, on the "real" entry into that code, the static members were not null, but not fully initialized.
Ryan Steckler
Senscom, Inc.
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1aef87733d76036298a6fe@msnews.microsoft.c om...[color=blue]
> <"Ryan Steckler" <rsteckler at s_e_n_s_c_o_m dot com (remove all
> underscores)>> wrote:[color=green]
> > I found this behavior while trying to implement a singleton class. Below is
> > a somewhat more straight forward example, though admittedly less useful in
> > real life. The basic problem is that when a static property of a class is
> > called (StaticInit.MyProperty), the property code executes BEFORE the static
> > members are initialized. This would lead one to assume that the static
> > members would equate to null, but that isn't the case either. They are in
> > some limbo state of uninitialization. Below is a simple class showing the
> > problem:[/color]
>
> <snip>
>
> I cannot reproduce your problem. Static members should be null before
> any extra initialization takes place.
>
> Could you give a *complete* example which demonstrates the problem?
>
> See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
>
> Note that if a class is marked with beforefieldinit, which is will be
> unless you've got a static constructor (as opposed to just static
> variable initializers), a static property *can* start executing before
> the static initializers are run - although in my experience they're
> not. However, the static initializers *will* be run before the first
> field access.
>
> See
http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
> information on that.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]