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
274 comments

Comments navigation

Older comments
Newer comments
  1. AlbertGup says:
    16. October 2025 at 19:15

    https://t.me/Official_1xbet_1xbet/s/104

  2. AlbertGup says:
    16. October 2025 at 19:40

    https://t.me/Official_1xbet_1xbet/s/105

  3. AlbertGup says:
    16. October 2025 at 20:06

    https://t.me/Official_1xbet_1xbet/s/481

  4. AlbertGup says:
    16. October 2025 at 20:32

    https://t.me/Official_1xbet_1xbet/s/1017

  5. AlbertGup says:
    16. October 2025 at 20:57

    https://t.me/Official_1xbet_1xbet/s/1523

  6. AlbertGup says:
    16. October 2025 at 21:23

    https://t.me/Official_1xbet_1xbet/s/1078

  7. AlbertGup says:
    16. October 2025 at 21:50

    https://t.me/Official_1xbet_1xbet/s/480

  8. AlbertGup says:
    16. October 2025 at 22:17

    https://t.me/Official_1xbet_1xbet/s/1020

  9. AlbertGup says:
    16. October 2025 at 22:45

    https://t.me/Official_1xbet_1xbet/s/851

  10. AlbertGup says:
    16. October 2025 at 23:12

    https://t.me/Official_1xbet_1xbet/s/662

  11. AlbertGup says:
    16. October 2025 at 23:39

    https://t.me/Official_1xbet_1xbet/s/554

  12. AlbertGup says:
    17. October 2025 at 00:06

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

  13. AlbertGup says:
    17. October 2025 at 00:31

    https://t.me/Official_1xbet_1xbet/s/221

  14. AlbertGup says:
    17. October 2025 at 00:57

    https://t.me/Official_1xbet_1xbet/s/216

  15. AlbertGup says:
    17. October 2025 at 01:25

    https://t.me/Official_1xbet_1xbet/s/1242

  16. AlbertGup says:
    17. October 2025 at 01:53

    https://t.me/Official_1xbet_1xbet/s/842

  17. AlbertGup says:
    17. October 2025 at 02:20

    https://t.me/Official_1xbet_1xbet/s/1444

  18. AlbertGup says:
    17. October 2025 at 02:47

    https://t.me/Official_1xbet_1xbet/s/308

  19. AlbertGup says:
    17. October 2025 at 03:16

    https://t.me/Official_1xbet_1xbet/s/135

  20. AlbertGup says:
    17. October 2025 at 03:44

    https://t.me/Official_1xbet_1xbet/s/517

  21. AlbertGup says:
    17. October 2025 at 04:11

    https://t.me/Official_1xbet_1xbet/s/1434

  22. AlbertGup says:
    17. October 2025 at 04:36

    https://t.me/Official_1xbet_1xbet/s/1223

  23. AlbertGup says:
    17. October 2025 at 05:01

    https://t.me/Official_1xbet_1xbet/s/729

  24. AlbertGup says:
    17. October 2025 at 05:30

    https://t.me/Official_1xbet_1xbet/s/604

  25. AlbertGup says:
    17. October 2025 at 05:59

    https://t.me/Official_1xbet_1xbet/s/501

  26. AlbertGup says:
    17. October 2025 at 06:24

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

  27. AlbertGup says:
    17. October 2025 at 06:52

    https://t.me/Official_1xbet_1xbet/s/1231

  28. glpro says:
    17. October 2025 at 07:18

    **glpro**

    glpro is a natural dietary supplement designed to promote balanced blood sugar levels and curb sugar cravings.

  29. AlbertGup says:
    17. October 2025 at 07:20

    https://t.me/Official_1xbet_1xbet/s/713

  30. AlbertGup says:
    17. October 2025 at 07:46

    https://t.me/Official_1xbet_1xbet/s/619

  31. sugarmute says:
    17. October 2025 at 07:52

    **sugarmute**

    sugarmute is a science-guided nutritional supplement created to help maintain balanced blood sugar while supporting steady energy and mental clarity.

  32. AlbertGup says:
    17. October 2025 at 08:12

    https://t.me/Official_1xbet_1xbet/s/351

  33. AlbertGup says:
    17. October 2025 at 08:38

    https://t.me/Official_1xbet_1xbet/s/1550

  34. AlbertGup says:
    17. October 2025 at 09:06

    https://t.me/Official_1xbet_1xbet/s/1358

  35. JosephabolF says:
    17. October 2025 at 09:57

    https://t.me/Official_1xbet_1xbet/1760

  36. JosephabolF says:
    17. October 2025 at 10:27

    https://t.me/s/Official_1xbet_1xbet/1717

  37. JosephabolF says:
    17. October 2025 at 10:55

    https://t.me/s/Official_1xbet_1xbet/1739

  38. JosephabolF says:
    17. October 2025 at 11:25

    https://t.me/Official_1xbet_1xbet/1602

  39. JosephabolF says:
    17. October 2025 at 11:55

    https://t.me/s/Official_1xbet_1xbet/1758

  40. JosephabolF says:
    17. October 2025 at 12:25

    https://t.me/s/Official_1xbet_1xbet/1605

  41. JosephabolF says:
    17. October 2025 at 12:53

    https://t.me/s/Official_1xbet_1xbet/1755

  42. vitta burn says:
    17. October 2025 at 13:09

    **vitta burn**

    vitta burn is a liquid dietary supplement formulated to support healthy weight reduction by increasing metabolic rate, reducing hunger, and promoting fat loss.

  43. JosephabolF says:
    17. October 2025 at 13:23

    https://t.me/Official_1xbet_1xbet/1809

  44. JosephabolF says:
    17. October 2025 at 13:54

    https://t.me/Official_1xbet_1xbet/1715

  45. synaptigen says:
    17. October 2025 at 14:06

    **synaptigen**

    synaptigen is a next-generation brain support supplement that blends natural nootropics, adaptogens

  46. glucore says:
    17. October 2025 at 14:16

    **glucore**

    glucore is a nutritional supplement that is given to patients daily to assist in maintaining healthy blood sugar and metabolic rates.

  47. JosephabolF says:
    17. October 2025 at 14:22

    https://t.me/s/Official_1xbet_1xbet/1741

  48. prodentim says:
    17. October 2025 at 14:48

    **prodentim**

    prodentim an advanced probiotic formulation designed to support exceptional oral hygiene while fortifying teeth and gums.

  49. JosephabolF says:
    17. October 2025 at 14:53

    https://t.me/s/Official_1xbet_1xbet/1724

  50. JosephabolF says:
    17. October 2025 at 15:23

    https://t.me/Official_1xbet_1xbet/1614

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.