0

Lots of answers for similar questions seem to always be about static classes. This is about instance fields.

Suppose I have this

// File.cs
public partial class Stuff 
{
    public Stuff()
    {
        obj.DoSomething();
    }
}

// OtherFile.cs
public partial class Stuff 
{
    MyObject obj = new MyObject("test");
}

Is this guaranteed to be safe? Like it won't do, for example, field initialization and run the constructor in one class, and then do the field initialization in the other part?

Does the C# language guarantee this will always be safe?

Note: Is the "textual order" across partial classes formally defined? does not answer the question. The answer for the current question gives an answer that is not part of such a question. It should not be linked as a duplicate to this particular link.

Water
  • 3,245
  • 3
  • 28
  • 58
  • 2
    It won't do anything "in the other part". Partial classes are a purely syntactical trick to split definitions; it still ends up as one class on the IL level. The only thing that could be affected by it being partial is the order of declarations (there's probably rules for how those are resolved, but I'm too lazy to look them up) but those are not relevant here, because field initializers always happen before the constructor. – Jeroen Mostert Jun 24 '21 at 18:37
  • _"The answer for the current question gives an answer that is not part of such a question"_ -- wrong. Both answers in the duplicate direct you straight to the specification. The second answer even quotes _"within each part the field initializers are executed in order"_, which makes clear that other than the indeterminate order between partial files, field initialization occurs exactly as it would otherwise. See also https://stackoverflow.com/a/431221 – Peter Duniho Jun 25 '21 at 21:26
  • @PeterDuniho -- wrong. The first accepted answer in the duplicate doesn't answer it at all and only says the order is undefined according to the spec. The second one says that the field initializers are executed in order, but that doesn't answer my question because it's only a fraction of it, but its clearly not the entire answer (and arguably not even useful). The second one is about static variables (not instance) and still doesn't answer it. Jon Skeet's post doesn't help either. My accepted answer fills in the missing pieces that all of the stuff you're talking about does _not_ have. – Water Jun 26 '21 at 00:50

1 Answers1

2

Yes, because partial classes are still compiled into one class. The partial syntax feature just lets you split the definition of a class into multiple files. There are no "parts" as far as the compiled type is concerned.

So all sequencing regarding static and non-static constructors, initializers, etc. still hold.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Just to cite sources, section 15.2.7 Partial declarations of ECMA-334 says: `All parts of a partial type shall be compiled together such that the parts can be merged at compile-time`. – cassandrad Jun 25 '21 at 22:37