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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:04:49+00:00 2026-05-15T13:04:49+00:00

Which static class initializes first if we have one more static classes in our

  • 0

Which static class initializes first if we have one more static classes in our project?

For example: Below code gives null exception.

class Program
    {
        static void Main(string[] args)
        {
            First.Write();
            Second.Write();
        }
    }
    static class First
    {
        public static int[] firstArray = new int[20];
        public static int[] secondArray = Second.secondArray;
        public static void Write()
        {
            Console.WriteLine(firstArray.ToString());
            Console.WriteLine(secondArray.ToString());
        }
    }
    static class Second
    {
        public static int[] firstArray = First.firstArray;
        public static int[] secondArray = new int[30];
        public static void Write()
        {
            Console.WriteLine(firstArray.ToString());
            Console.WriteLine(secondArray.ToString());
        }
    }

If you pay attention, you will see that if First class will initialize itself so secondArray field of Second would be null. But if Second class would initialize first so Second class firstArray would be null. I am trying to tell that which initialize first makes different results.

I think that it is abstract question about my project. I encounter it while trying to understand why I am getting unexpected results.

  • 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-15T13:04:49+00:00Added an answer on May 15, 2026 at 1:04 pm

    First will begin to initialize, assign firstArray, then notice that it requires Second to be initialized in order to get the initial value of secondArray.

    Second will start initializing, and then notice that it requires First to be initialized. However, the CLR will then notice that First is already initializing in the current thread, so it won’t block. Second‘s initialization will complete, and then First’s initialization will complete.

    Fortunately, the field that Second needs has already been assigned, so the “right thing” happens.

    That’s all very well if First actually starts initializing first. However, as neither of the classes has a static constructor, it’s possible that Second will start to initialize first… it would then start to initialize First, which would spot that Second is already initializing and take Second.secondArray‘s current value (null) for First.secondArray. This would be a Bad Thing. Note that the initialization timing for types without static constructors has changed in .NET 4 – not in a spec-breaking way, but possibly in an existing-code-breaking way.

    If both First and Second had static constructors, then First would be initialized first, as that’s the first class that Main touches.

    Moral of the answer: don’t do this. Type initializers which refer to each other are very error prone. For another example, see Eric Lippert and Neal Gafter’s NDC 2010 talk, “C# Puzzlers” which can be viewed on the NDC video page.

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

Sidebar

Related Questions

I have a static class in a shared project, which I want to extend
Which one is more acceptable (best-practice)?: namespace NP public static class IO public static
In my testing project, I have a static class called FixtureSetup which I use
I have a class in which i have intialized hashmap in static block. Passing
This has defeated me. I want to have a static class variable which is
I have a class with static method which has a local static variable. I
I have a static timer class which will be called by ANY webpage to
Java Naming rules suggests which one to be used. This public static final class
I have a class which contains a static collection that can be used across
I have a static Command class like this (but with many more commands): class

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.