ph91.de
  • Home
  • About me
  • Categories
    • Software Development
    • New Zealand
    • Music
    • Tech & IT
    • Sports
    • Dublin
    • Junk
  • Contact
  • Disclaimer
ph91.de
ph91.de
  • Home
  • About me
  • Categories
    • Software Development
    • New Zealand
    • Music
    • Tech & IT
    • Sports
    • Dublin
    • Junk
  • Contact
  • Disclaimer
  • Software Development

Unexpected C# Behaviour

  • 16. September 2018
  • Philipp

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.

 

Related Topics
  • c#
  • compiler
  • ecma
  • language
  • static
Philipp

Previous Article
  • Tech & IT

Configure a postfix mailserver correctly (1blu)

  • 10. September 2018
  • Philipp
View Post
Next Article
  • Software Development

Design Patterns Game

  • 30. April 2019
  • Philipp
View Post
505 comments

Comments navigation

Older comments
Newer comments
  1. Georgedut says:
    27. October 2025 at 21:23

    https://t.me/s/bs_1Win/835

  2. Georgedut says:
    27. October 2025 at 23:43

    https://t.me/s/bs_1Win/696

  3. Georgedut says:
    28. October 2025 at 00:52

    https://t.me/bs_1Win/439

  4. Georgedut says:
    28. October 2025 at 02:03

    https://t.me/s/bs_1Win/964

  5. Georgedut says:
    28. October 2025 at 03:12

    https://t.me/s/bs_1Win/1080

  6. Georgedut says:
    28. October 2025 at 04:26

    https://t.me/s/bs_1Win/886

  7. Georgedut says:
    28. October 2025 at 05:40

    https://t.me/s/bs_1Win/1063

  8. Georgedut says:
    28. October 2025 at 06:53

    https://t.me/bs_1Win/320

  9. Georgedut says:
    28. October 2025 at 08:04

    https://t.me/bs_1Win/901

  10. Georgedut says:
    28. October 2025 at 09:15

    https://t.me/bs_1Win/528

  11. Georgedut says:
    28. October 2025 at 10:33

    https://t.me/s/bs_1Win/468

  12. CalvinQuarp says:
    28. October 2025 at 12:08

    https://t.me/s/Official_mellstroy_casino/9

  13. CalvinQuarp says:
    28. October 2025 at 13:23

    https://t.me/s/Official_mellstroy_casino/34

  14. CalvinQuarp says:
    28. October 2025 at 14:43

    https://t.me/s/Official_mellstroy_casino/22

  15. RouletteRogue says:
    28. October 2025 at 15:11

    https://t.me/s/Beefcasino_rus/30

  16. CalvinQuarp says:
    28. October 2025 at 15:55

    https://t.me/s/Official_mellstroy_casino/52

  17. CalvinQuarp says:
    28. October 2025 at 17:02

    https://t.me/Official_mellstroy_casino/24

  18. CalvinQuarp says:
    28. October 2025 at 18:18

    https://t.me/s/Official_mellstroy_casino/23

  19. CalvinQuarp says:
    28. October 2025 at 19:34

    https://t.me/Official_mellstroy_casino/20

  20. CalvinQuarp says:
    28. October 2025 at 20:38

    https://t.me/Official_mellstroy_casino/55

  21. CalvinQuarp says:
    28. October 2025 at 21:42

    https://t.me/Official_mellstroy_casino/58

  22. CalvinQuarp says:
    28. October 2025 at 22:43

    https://t.me/s/Official_mellstroy_casino/20

  23. CalvinQuarp says:
    28. October 2025 at 23:44

    https://t.me/Official_mellstroy_casino/26

  24. CalvinQuarp says:
    29. October 2025 at 00:45

    https://t.me/s/Official_mellstroy_casino/58

  25. AceSorcerer says:
    30. October 2025 at 04:17

    https://t.me/s/Best_promocode_rus/1248

  26. BluffMaster says:
    30. October 2025 at 17:02

    https://t.me/Best_promocode_rus/1304

  27. RoyalFlusher says:
    31. October 2025 at 00:49

    https://t.me/Beefcasino_rus/57

  28. MichaelSpers says:
    31. October 2025 at 05:10

    https://t.me/s/ud_Jet/62

  29. MichaelSpers says:
    31. October 2025 at 06:25

    https://t.me/ud_Booi/47

  30. MichaelSpers says:
    31. October 2025 at 07:39

    https://t.me/s/?ud_1Go/62

  31. MichaelSpers says:
    31. October 2025 at 08:54

    https://t.me/ud_Gama/45

  32. MichaelSpers says:
    31. October 2025 at 10:11

    https://t.me/s/ud_Vulkan/54

  33. MichaelSpers says:
    31. October 2025 at 11:30

    https://t.me/s/ud_Drip/61

  34. MichaelSpers says:
    31. October 2025 at 12:52

    https://t.me/s/ud_Booi/62

  35. MichaelSpers says:
    31. October 2025 at 14:10

    https://t.me/ud_Riobet/56

  36. MichaelSpers says:
    31. October 2025 at 15:31

    https://t.me/s/ud_Kometa/54

  37. MichaelSpers says:
    31. October 2025 at 16:50

    https://t.me/s/ud_Vodka/44

  38. MichaelSpers says:
    31. October 2025 at 18:12

    https://t.me/ud_PlayFortuna/63

  39. MichaelSpers says:
    31. October 2025 at 19:29

    https://t.me/ud_Gama/59

  40. MichaelSpers says:
    31. October 2025 at 20:45

    https://t.me/s/ud_Legzo/49

  41. MichaelSpers says:
    31. October 2025 at 22:00

    https://t.me/ud_1xSlots/62

  42. MichaelSpers says:
    31. October 2025 at 23:13

    https://t.me/ud_Flagman/64

  43. MichaelSpers says:
    1. November 2025 at 00:30

    https://t.me/ud_MostBet/46

  44. MichaelSpers says:
    1. November 2025 at 01:44

    https://t.me/ud_Booi/50

  45. AllInAce says:
    1. November 2025 at 02:05

    https://t.me/s/Beefcasino_rus/59

  46. MichaelSpers says:
    1. November 2025 at 05:25

    https://t.me/ud_Irwin/58

  47. MichaelSpers says:
    1. November 2025 at 06:38

    https://t.me/ud_Leon/61

  48. MichaelSpers says:
    1. November 2025 at 07:50

    https://t.me/ud_Stake/49

  49. MichaelSpers says:
    1. November 2025 at 09:02

    https://t.me/?ud_1Go/57

  50. MichaelSpers says:
    1. November 2025 at 10:17

    https://t.me/s/ud_Fresh/58

Comments navigation

Older comments
Newer comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Dev profiles
  • BitBucket
  • Github
  • Stackoverflow
Side Projects
  • NMF
  • Speed-kini.de
  • Sudoku Solver
  • Windwarner
Archive
ph91.de
Blog by Philipp Schreitmüller

Input your search keywords and press Enter.