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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:48+00:00 2026-05-27T15:01:48+00:00

Few weeks ago, I switched from Java to C#. Today, I had a weird

  • 0

Few weeks ago, I switched from Java to C#. Today, I had a weird behavior and I try to reproduce it in this simple sample. I’m using a .net FW 4.

I have three classes:
First, the abstract one:

namespace ReadonlyStaticOrder
{
    using System;
    using System.Collections.Generic;

    public abstract class AbstractClass
    {
        public AbstractClass(string value, IEnumerable<string> someValues)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (someValues == null)
            {
                throw new ArgumentNullException("someValues");
            }
            // would do something after...
        }
    }
}

The second one :

namespace ReadonlyStaticOrder
{
    using System.Collections.Generic;

    public sealed class ReadonlyOrderInitialization : AbstractClass
    {
        // this line introduces the bug, since it call the ctor before SomeValues already initialized
        // if removed, no more exception
        public static readonly ReadonlyOrderInitialization Sample = new ReadonlyOrderInitialization("sample");

        private static readonly IEnumerable<string> SomeValues = new string[] { "one", "two", "three" };

        public ReadonlyOrderInitialization(string value)
            : base(value, SomeValues)
        {
        }
    }
}

And the demonstrator:

namespace ReadonlyStaticOrder
{
    using System;

    public sealed class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new ReadonlyOrderInitialization("test");
            }
            catch (TypeInitializationException typeInitializationException)
            {
                Console.WriteLine(typeInitializationException.Message);
                Console.WriteLine(typeInitializationException.InnerException.Message);
                Console.WriteLine(typeInitializationException.StackTrace);
            }

            Console.ReadLine();
        }
    }
}

And the output is:

The type initializer for
‘ReadonlyStaticOrder.ReadonlyOrderInitialization’ threw an exception.
Value cannot be null. Parameter name: someValues at
ReadonlyStaticOrder.ReadonlyOrderInitialization..ctor(String value)
at ReadonlyStaticOrder.Program.Main(String[] args) in
d:\stackoverflow\static
readonlyissue\ConsoleApplication1\ReadonlyStaticOrder\Program.cs:line
12

I added a comment on the line which introduces the bug. For me, the compiler would have to warn me that the behavior can be weird because of the order of static initialization. Am I wrong?

Thank you guys and I hope you have enough information.

  • 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-27T15:01:48+00:00Added an answer on May 27, 2026 at 3:01 pm

    It is defined as text order – §17.11 in ECMA 334:

    If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor.

    As an aside, this gets particularly interesting if you consider partial classes, in which case : it is not defined.

    If in doubt, move the initialization explicitly to a static constructor.


    As for why; consider (note: these are just my own thoughts):

    • “definite assignment” is the thing that usually stops this being an issue, but “definite assignment” does not apply to fields
    • analysing the code in full detail is computationally complex (I’m thinking “halting”, perhaps) – so it could only offer an incomplete veneer of safety (which is artificial and could lead to problems in any non-trivial scenario)
    • and due to the partial classes issue, the full order itself is not strictly defined; so it cannot handle the general case – and again, covering a specific case (single class fragment etc) is back to the “thin veneer” (where it only warns for the obvious cases, but can’t help with the non-trivial ones)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked from few weeks ago this question: How can I teach a beginner
I moved from a shared hosting to a VPS a few weeks ago and
I'm really frustrated with this one. A few weeks ago I got it working
i asked this a few weeks ago, but couldnt get any of the suggested
I asked about this on the jquery forum a few weeks ago without luck,
I was using a version of v2 of protobuf-net from a few weeks ago
I was actually asked this myself a few weeks ago, whereas I know exactly
I am going back though a web-based document numbering system from few weeks ago.
Few weeks ago I decided to make an app, this app contained just video
This openvz box was working properly few weeks ago, and now it can't ping

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.