Expected output & output I get in debug mode, and release mode under VS2010, .NET 4.0:
bar construct
main
Output in release mode not under the VS2010 debugger, and under WinDbg:
main
Program does not exhibit this behavior on VS2005, .NET 2.0
using System;
namespace static_init
{
public class bar
{
public bar()
{
Console.WriteLine("bar construct");
}
}
class Program
{
public static bar blah = new bar();
static void Main(string[] args)
{
Console.WriteLine("main");
Console.ReadLine();
}
}
}
Probably related: http://stackoverflow.com/questions/2925611/static-constructor-can-run-after-the-non-static-constructor-is-this-a-compiler
Update
In my actual code constructor bar() initializes some interop code with C++ ( unmanaged ). It needs to happen before anything else in this library - is there any way to ensure that without putting in an init() function that touches all of the statics ( with side effects that arn't externally referenced ) in the library?
Note for future searchers: I'm using SWIG, and this is an assumption that they made in their wrapper generation code. SWIGStringHelper is the current offender, there may be more though.
Conclusion
Update to version 2.0 of SWIG, it puts in the static constructor as needed by newer version of .NET.