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

  • Home
  • SEARCH
  • 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 8216333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:05:31+00:00 2026-06-07T12:05:31+00:00

During my research into the best way to build a Singleton in C# I

  • 0

During my research into the best way to build a Singleton in C# I stumbled across the following article where there is a brief mention that in C++

“The C++ specification left some ambiguity around the initialization
order of static variables.”

I ended up looking into the question and found this and this. Where basically the point (as far as I understand) is that the initialization order of static variables in C++ is undefined. Ok I guess so far so good, but then I wanted to understand the following statement that the article later makes

“Fortunately, the .NET Framework resolves this ambiguity through its
handling of variable initialization.”

So I found this page where they say

The static field variable initializers of a class correspond to a
sequence of assignments that are executed in the textual order in
which they appear in the class declaration.

and give the example of

using System;
class Test
{
   static void Main() {
      Console.WriteLine("{0} {1}", B.Y, A.X);
   }
   public static int F(string s) {
      Console.WriteLine(s);
      return 1;
   }
}
class A
{
   static A() {}
   public static int X = Test.F("Init A");
}
class B
{
   static B() {}
   public static int Y = Test.F("Init B");
}

the output must be: 
Init B 
Init A
1 1

“Because the rules for when static constructors execute (as defined in
Section 10.11) provide that B’s static constructor (and hence B’s
static field initializers) must run before A’s static constructor and
field initializers.”

But where I am confused is that my understanding was that the initialization order of static variables in these examples would be based on when a method or field within the class was first invoked, which is in turn based on the execution order of the block of code (this case left to right). IE: Completely independent of where – or the order – of the class declaration. Yet by my interpretation of that article it says its as a result of the order of declaration of those classes, which my testing doesn’t back up?

Could someone please clarify this (and the point the article is trying to make) for me and perhaps provide a better example that illiterates the behaviour described?

  • 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-07T12:05:33+00:00Added an answer on June 7, 2026 at 12:05 pm

    The static field variable initializers of a class correspond to a
    sequence of assignments that are executed in the textual order in
    which they appear in the class declaration.

    This means that within the same class, static fields are initialized in order of appearance in the source code. For example:

    class A
    {
       public static int X = Test.F("Init A.X");
       public static int Y = Test.F("Init A.Y");
    }
    

    When it’s time for the static fields to be initialized, X is guaranteed to be initialized before Y.

    “Because the rules for when static constructors execute (as defined in
    Section 10.11) provide that B’s static constructor (and hence B’s
    static field initializers) must run before A’s static constructor and
    field initializers.”

    This means that the static constructor and member initialization for each class will run in evaluation order when expressions that access these classes appear¹. The relative order of appearance of the class definitions in source code does not play any role, even if they appear in the same source file (which they most certainly are not obliged to do). For example:

    static void Main() {
        Console.WriteLine("{0} {1}", B.Y, A.X);
    }
    

    Assuming that neither A nor B has already been statically initialized, order of evaluation guarantees that all the fields of B will be initialized before any field of A. The fields of each class will be initialized in the order specified by the first rule.


    ¹ for the purposes of this discussion I am ignoring the existence of beforefieldinit.

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

Sidebar

Related Questions

I understand from my research that IE8 does annoying things like forcing itself into
I've created a set of JSON data that gets passed into IndexedDB during the
I’m looking at creating a P2P system. During initial research, I’m reading from Peer-to-Peer
During our build process, we run sqlldr to populate our database with some sample
In short, I am currently working on a research problem for my university that
I tried to research this, but there were still some questions left unanswered. I
During my research I have found opinions pointing in both directions on this issue.
I've build several websites using PHP and mySQL as backend, and believe that I'm
I'm in the research stage of a project that will require the development of
I am seeking pointers to assist the direction of my research into providing assessements

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.