by andygocke · 1y ago · 0 comments
Let's be clear: breaking dozens of tools because of a change to the IL format also outsources an issue to all C# devs. The .NET IL format has been basically unchanged since .NET 2.0 and huge numbers of people take very hard dependencies on the exact things they do and do not expect. I don't expect we would have been able to make significant changes due to the breaking change impact.
> A possible approach I mentioned
This would likely be even harder to understand. For better or worse, the .NET design is that external initializers happen _after_ the constructor runs. That's been true all the way back to when the initializer syntax was first introduced in C# 3. Making regular initializers and `with` initializers have inverted order strikes me as being way worse.
If I could go back in time, I think the main change to C# I would make would be to enforce that the constructor always runs after all external initialization.
What am I missing?
Internal initializers are run as part of the constructor, before any user code. External initializers are run after the constructor, on the constructed object.
For instance:
class C
{
public int P = 5;
}
var c = new C { P = 3 };
`c.P` has the value 3.In your example:
var n2 = n1.<Clone>$();
n2.Value = 3; // 'with' field setters
n2.<OnPostCloneInitialise>(); // run the initialisers
The “PostCloneInitializers” you’re running are the field initializers, so the order is backwards. You’re overwriting the value of the external initializers with the internal initializers.