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. MichaelSpers says:
    1. November 2025 at 11:30

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

  2. MichaelSpers says:
    1. November 2025 at 12:45

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

  3. MichaelSpers says:
    1. November 2025 at 14:12

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

  4. MichaelSpers says:
    1. November 2025 at 15:25

    https://t.me/ud_Irwin/27

  5. MichaelSpers says:
    1. November 2025 at 17:42

    https://t.me/s/UD_BOoI

  6. MichaelSpers says:
    1. November 2025 at 18:58

    https://t.me/s/uD_dRagonMOneY

  7. MichaelSpers says:
    1. November 2025 at 20:12

    https://t.me/s/Ud_catcasINo

  8. MichaelSpers says:
    1. November 2025 at 22:25

    https://t.me/s/Ud_MRbiT

  9. MichaelSpers says:
    1. November 2025 at 23:27

    https://t.me/s/Ud_IZZI

  10. MichaelSpers says:
    2. November 2025 at 00:31

    https://t.me/s/uD_daddy

  11. MichaelSpers says:
    2. November 2025 at 01:36

    https://t.me/s/UD_iRWIn

  12. RouletteRogue says:
    2. November 2025 at 02:13

    https://t.me/s/official_1win_aviator/38

  13. MichaelSpers says:
    2. November 2025 at 02:39

    https://t.me/s/Beefcasino_rus

  14. MichaelSpers says:
    2. November 2025 at 03:41

    https://t.me/s/Ud_rIoBet

  15. RoyalFlusher says:
    2. November 2025 at 05:16

    https://t.me/s/official_1win_aviator/95

  16. MichaelSpers says:
    2. November 2025 at 05:56

    https://t.me/s/ud_1Go

  17. MichaelSpers says:
    2. November 2025 at 07:01

    https://t.me/s/UD_VULKAN

  18. MichaelSpers says:
    2. November 2025 at 08:08

    https://t.me/s/uD_CASinO_X

  19. MichaelSpers says:
    2. November 2025 at 10:22

    https://t.me/s/ud_StaKe

  20. Caseydrusy says:
    2. November 2025 at 11:48

    https://t.me/s/tf_1win

  21. AceSorcerer says:
    2. November 2025 at 12:41

    https://t.me/s/official_1win_aviator/78

  22. StevenGok says:
    2. November 2025 at 14:11

    https://t.me/s/tf_1win

  23. StevenGok says:
    2. November 2025 at 15:13

    https://t.me/s/kta_1win

  24. StevenGok says:
    2. November 2025 at 16:10

    https://t.me/s/kfo_1win

  25. AlbertDioke says:
    2. November 2025 at 18:20

    https://t.me/s/Ud_StarDa

  26. AlbertDioke says:
    2. November 2025 at 19:33

    https://t.me/s/Ud_CatCasINo

  27. AlbertDioke says:
    2. November 2025 at 20:42

    https://t.me/s/UD_PInco

  28. AlbertDioke says:
    2. November 2025 at 21:46

    https://t.me/s/ud_MRbIt

  29. AlbertDioke says:
    2. November 2025 at 22:48

    https://t.me/s/uD_mArTIN

  30. AlbertDioke says:
    2. November 2025 at 23:50

    https://t.me/s/Ud_joYCASino

  31. AlbertDioke says:
    3. November 2025 at 00:53

    https://t.me/s/ud_jeT

  32. AlbertDioke says:
    3. November 2025 at 01:54

    https://t.me/s/ud_keNT

  33. AlbertDioke says:
    3. November 2025 at 02:56

    https://t.me/s/uD_KomEtA

  34. AlbertDioke says:
    3. November 2025 at 03:59

    https://t.me/s/ud_gIZbo

  35. AlbertDioke says:
    3. November 2025 at 05:02

    https://t.me/s/ud_FRESh

  36. AlbertDioke says:
    3. November 2025 at 06:10

    https://t.me/s/uD_LEgzO

  37. AlbertDioke says:
    3. November 2025 at 07:19

    https://t.me/s/ud_MOstBeT

  38. AlbertDioke says:
    3. November 2025 at 08:28

    https://t.me/s/uD_StAkE

  39. JamesBlade says:
    3. November 2025 at 19:54

    https://t.me/s/ke_GGBet

  40. JamesBlade says:
    3. November 2025 at 21:06

    https://t.me/s/ke_Flagman

  41. JamesBlade says:
    3. November 2025 at 22:17

    https://t.me/s/ke_Stake

  42. RoyalFlusher says:
    3. November 2025 at 23:16

    https://t.me/s/official_1win_aviator/82

  43. JamesBlade says:
    3. November 2025 at 23:28

    https://t.me/s/ke_MostBet

  44. JamesBlade says:
    4. November 2025 at 00:34

    https://t.me/s/ke_Pinco

  45. JamesBlade says:
    4. November 2025 at 01:45

    https://t.me/s/ke_kent

  46. JamesBlade says:
    4. November 2025 at 02:53

    https://t.me/s/ke_Pin_Up

  47. JamesBlade says:
    4. November 2025 at 03:58

    https://t.me/s/ke_Starda

  48. JamesBlade says:
    4. November 2025 at 05:04

    https://t.me/s/ke_Gizbo

  49. JamesBlade says:
    4. November 2025 at 06:11

    https://t.me/s/ke_Booi

  50. JamesBlade says:
    4. November 2025 at 08:18

    https://t.me/s/ke_Martin

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.