I recently stumbled across the unexpected behavior of C# when dealing with static variables. I put it a green unit test:
private static string a = $"a{b}";
private static string b = $"b{2+100}";
private static string c = $"{b}c";
[Test]
public void Test()
{
Assert.AreEqual("a",a);
Assert.AreEqual("b102c", c);
Assert.AreEqual(10, ReturnSum()(2,3));
}
As C# has an ECMA definition, this behavior is described in there (reference found here):
17.11: The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
- An instance of the class is created.
- Any of the static members of the class are referenced.
If a class contains the Main method (§10.1) in which execution begins, the static constructor for that class executes before the Main method is called. If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor (§17.4.5).
Despite that, it is quite weird that the compiler doesn’t warn you if a static member is referencing a static member which is executed later.
298 comments
**mind vault**
mind vault is a premium cognitive support formula created for adults 45+. It’s thoughtfully designed to help maintain clear thinking
https://t.me/s/Online_1_xbet/1097
https://t.me/s/Online_1_xbet/2354
https://t.me/s/Official_1xbet_1xbet
https://t.me/s/rating_online/13
https://t.me/rating_online/5
https://t.me/rating_online/4
https://t.me/rating_online/3
https://t.me/s/rating_online/4
https://t.me/rating_online/1
https://t.me/rating_online/8
https://t.me/s/rating_online/3
https://t.me/Online_1_xbet/2114
https://t.me/Online_1_xbet/1961
https://t.me/Online_1_xbet/2491
https://t.me/Online_1_xbet/2130
https://t.me/Online_1_xbet/2751
https://t.me/Online_1_xbet/1977
https://t.me/Online_1_xbet/1838
https://t.me/Online_1_xbet/1944
https://t.me/Online_1_xbet/3444
https://t.me/Online_1_xbet/2825
https://t.me/Online_1_xbet/2876
https://t.me/Official_1xbet_1xbet/s/539
https://t.me/Official_1xbet_1xbet/s/860
https://t.me/Official_1xbet_1xbet/s/700
https://t.me/Official_1xbet_1xbet/s/463
https://t.me/Official_1xbet_1xbet/s/1232
https://t.me/Official_1xbet_1xbet/s/1525
https://t.me/Official_1xbet_1xbet/s/981
https://t.me/Official_1xbet_1xbet/s/661
https://t.me/Official_1xbet_1xbet/s/346
https://t.me/Official_1xbet_1xbet/s/1108
https://t.me/Official_1xbet_1xbet/s/804
https://t.me/Official_1xbet_1xbet/s/892
https://t.me/Official_1xbet_1xbet/s/1218
https://t.me/Official_1xbet_1xbet/s/1071