Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:39:09+00:00 2026-05-16T10:39:09+00:00

Expected output & output I get in debug mode, and release mode under VS2010,

  • 0

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:
Static constructor can run after the non-static constructor. Is this a compiler bug?

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 aren’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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:39:09+00:00Added an answer on May 16, 2026 at 10:39 am

    It’s probably getting optimized out because you don’t use it.

    It also isn’t a compiler bug, it’s in the language spec.

    17.4.5.1 Static field initialization

    The static field variable initializers
    of a class declaration correspond to a
    sequence of assignments that are
    executed in the textual order in which
    they appear in the class declaration.
    If a static constructor (§17.11)
    exists in the class, execution of the
    static field initializers occurs
    immediately prior to executing that
    static constructor. Otherwise, the
    static field initializers are executed
    at an implementation-dependent time
    prior to the first use of a static
    field of that class

    Since you never use a static field of the Program class, the static initializer isn’t guaranteed to run (though it could…the ‘implementation-dependent time’ above)

    Update
    You can accomplish what you want by making Program have a static constructor.

    static Program (){}
    or possibly by accessing another (possibly dummy) static variable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My LINQ query is not producing the expected output below. Basically, it's the sum
This code when executed displays the expected output but prints segmentation fault (core dumped)
$fr = hammad; $frhammad = nuthing; echo $fr{$fr}; Output: h Whereas the expected output
I expected to see the word test appear in the output once and the
From the following code, I expect to get this output from the corresponding input:
I want to get all leaf-nodes(ID & text of node) from jsTree ? I
Expected Outcome: Successful installtion of Plone 4.1.4 Actual Outcome: Installation fails with: libxml2: cmmi
I expected to find XMLStreamReader to be AutoCloseable in Java 7. However, that is
I expected these generators to be available: $ rails g Usage: rails generate GENERATOR
I expected this to print [b] but it prints [] : $x = abc;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.