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 8653329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:30:59+00:00 2026-06-12T14:30:59+00:00

If a type has no static constructor, field initializers will execute just prior to

  • 0

If a type has no static constructor, field initializers will execute
just prior to the type being used— or anytime earlier at the whim of
the runtime

Why this code :

void Main()
{ 
  "-------start-------".Dump();
   Test.EchoAndReturn("Hello");
  "-------end-------".Dump();

}

 class Test
{
    public static string x = EchoAndReturn ("a");
    public static string y = EchoAndReturn ("b");
    public static string EchoAndReturn (string s)
    {
        Console.WriteLine (s);
        return s;
    }
}

yields :

-------start-------
a
b
Hello
-------end-------

while this code :

void Main()
{ 
  "-------start-------".Dump();
   var test=Test.x;
  "-------end-------".Dump();

}

yields

a
b
-------start-------
-------end-------

The order of a and b is understood. but why dealing with static method is different than static field.

I mean why the start and end lines are in different locations with static methods vs static fields ? I mean – in both situation he’s got to initialize those fields…so why ?

( I know I can add static ctor which make it to be the same – but Im asking about this particular situation. )

(p.s. Dump() is just like console.write)

  • 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-06-12T14:31:00+00:00Added an answer on June 12, 2026 at 2:31 pm

    The behavior of the release JIT is (from 4.0 IIRC) not to run the static initializer unless the method you are calling touches static fields. This can mean the static fields are not initialized. If I run your first code in release outside of the debugger, I get:

    -------start-------
    Hello
    -------end-------
    

    If I run it with the debugger attached (release), or in a debug build (with or without debugger attached), I get:

    -------start-------
    a
    b
    Hello
    -------end-------
    

    So far so interesting. For why you get:

    a
    b
    -------start-------
    -------end-------
    

    it looks like the per-method JIT is essentially taking responsibility for running the static constructor in this scenario. You can see this by adding:

    if(NeverTrue()) { // method that returns false
            "-------start-------".Dump();
            var test = Test.x;
            "-------end-------".Dump();
    }
    

    which will print (even in release without the debugger)

    a
    b
    

    so the possibility of accessing fields is key. If we change Test.x to be a call to a method that doesn’t access fields (and remove the NeverTrue() thing), then we get no output whatsoever.

    So: in some versions of the CLI, the execution of static initializers may be deferred to the JIT-step of methods that contain mentions to any field (it doesn’t check whether that field has an initializer).

    We can even create instances of objects without running the static initializer, as long as we don’t touch static fields:

     public Test()
     {
         a = "";
     }
     string a;
    

    with:

    "-------start-------".Dump();
    new Test();
    "-------end-------".Dump();
    

    prints just (release, no debugger):

    -------start-------
    -------end-------
    

    HOWEVER! We should not build anything that depends on this timing:

    • it changes between .NET version
    • it may well change between platform (x86, x64, CF, SL, .NETCore, etc)
    • it can change depending on whether the debugger is attached and whether it is a debug/release build
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Content type Events has intro, body and an event date field (using Date module).
From MSDN ( link ): The type PrintDocumentImageableArea has no public constructor. Hence, the
I am trying to register a type that has a constructor method like: public
I've read that the StringBuilder type has a limit (the default is 16 characters),
I know the following is not possible because the Enumeration's type has to be
I have a collection FileTypes that holds objects of type FileType. That type has
Assuming server control of type A has a protected member M , then we
I created a content type which has some fields and i want this fields
When I am running this program I am getting warning array subscript has type
I'm struggling to understand what precisely does it mean when a value has type

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.