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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:19:16+00:00 2026-05-26T15:19:16+00:00

Specifically, in relation to field initializers (in this case, static) – §17.11 in ECMA

  • 0

Specifically, in relation to field initializers (in this case, static) – §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.

Now, if we have multiple partial classes in separate files, is that order determined anywhere? My gut says “not formally defined, but probably relates to the order included in the csproj, or the order noted to csc”. Is this correct?

(and yes, I realise it would be better to avoid the ambiguity completely – probably by moving all the initialization to a static constructor).


For example, if I have a.cs:

using System;
partial class Program
{
    private static int Foo = Write("Foo");
    static int Write(string name)
    {
        Console.WriteLine(name);
        return 0;
    }
    static void Main()
    {
        Console.WriteLine("[press any key]");
        Console.ReadLine();
    }
}

and b.cs:

partial class Program
{
    private static int Bar = Write("Bar");
}

and:

<Compile Include="a.cs" />
<Compile Include="b.cs" />

then this is Foo then Bar; if, however, this is:

<Compile Include="b.cs" />
<Compile Include="a.cs" />

then it is Bar then Foo. This supports the observation, but does not state it strongly. §8.7.13 (Partial type declarations) makes no comment on the order when combining partial classes. So; is there anything stronger we can say here, either from the C# language spec or from the tooling documentation?

Additionally, it behaves similarly with csc a.cs b.cs vs csc b.cs a.cs.

  • 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-26T15:19:16+00:00Added an answer on May 26, 2026 at 3:19 pm

    Here’s another snippet from the C# spec which, taken with your snippet, appears to settle that this is undefined behaviour:

    10.2.6 Members

    […] The ordering of members within a type is rarely significant to
    C# code, but may be significant when interfacing with other languages
    and environments. In these cases, the ordering of members within a
    type declared in multiple parts is undefined.

    To be fair, it does say rarely significant, not insignificant :).

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

Sidebar

Related Questions

Specifically, I have a model that has a field like this pub_date = models.DateField(date
Specifically this is regarding when using a client session cookie to identify a session
Specifically, is the following legal C++? class A{}; void foo(A*); void bar(const A&); int
I am just beginning to learn about accessbility specifically in relation to JAWS -
I'm just curious if this is possible, specifically in SQL CE (Express) with support
I am looking into WCF specifically in relation to Silverlight. Can someone tell me
Specifically I want all PasswordBox's using a Style to have the same static KeyDown
I've got a web app which has pretty intensive processor requirements, specifically in relation
Specifically, a class or id within the css. Would you use something similar to
I'm asking this in relation to Flex Builder, but it might apply to Eclipse

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.