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.
505 comments
https://t.me/s/official_Martin_es
https://t.me/s/official_MostBet_ed
https://t.me/s/official_Vulkan_ed
https://t.me/s/official_PlayFortuna_es
https://t.me/s/official_Pinco_es
https://t.me/s/official_CasinoX_ed
https://t.me/s/official_PlayFortuna_ed
https://t.me/s/official_Pinco_ed
https://t.me/s/official_CatCasino_ed
https://t.me/s/official_Riobet_es
https://t.me/s/official_Fresh_es
https://t.me/s/official_JoyCasino_ed
https://t.me/s/official_JoyCasino_es
https://t.me/s/official_Gama_ed
https://t.me/s/official_Sol_ed
https://t.me/Starda_egs/22
https://t.me/s/GGBet_egs/17
https://t.me/s/Drip_egs/9
https://t.me/s/iGaming_live/4605
https://t.me/s/Riobet_egs/21
https://t.me/Kometa_egs/10
https://t.me/MostBet_egs/14
https://t.me/s/PinUp_egs/19
https://t.me/Booi_egs/4
https://t.me/s/R7_egs/9
https://t.me/Vodka_egs/16
https://t.me/s/CatCasino_egs/16
https://t.me/Vulkan_egs/13
https://t.me/s/Stake_egs/3
https://t.me/Leon_egs/17
https://t.me/MostBet_egs/9
https://t.me/Riobet_egs/5
https://t.me/s/Rox_egs/18
https://t.me/Starda_egs/5
https://t.me/GGBet_egs/7
https://t.me/s/Daddy_egs/17
https://t.me/Riobet_egs/4
https://t.me/s/CasinoX_egs/3
https://t.me/s/?@DragonMoney_egs/20
https://t.me/s/PinUp_egs/9
https://t.me/va_1xbet/16
https://t.me/iGaming_live/4643
https://t.me/va_1xbet/3
https://t.me/va_1xbet/18
https://t.me/va_1xbet/4
https://t.me/s/va_1xbet/9
https://t.me/s/va_1xbet
https://t.me/va_1xbet/5
https://t.me/va_1xbet/22
https://t.me/s/va_1xbet/14