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
13 comments
  1. Pingback: order cheap enclomiphene online
  2. Pingback: sans ordonnance kamagra en spain
  3. Pingback: androxal with consult
  4. Pingback: cheap flexeril cyclobenzaprine generic united states
  5. Pingback: cheap dutasteride cost on prescription
  6. Pingback: how to order gabapentin cheap generic uk
  7. Pingback: buy cheap fildena generic canada no prescription
  8. Pingback: online order itraconazole non prescription online
  9. Pingback: buy cheap staxyn canada cost
  10. Pingback: purchase avodart buy dallas
  11. Pingback: buy xifaxan price usa
  12. Pingback: get rifaximin generic buy online
  13. Pingback: kamagra online bez lékařského předpisu nebo členství

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.