C #: Etudes, part 2

    Continuation, the beginning here.

    I will begin with a social survey: what methods do you know to execute the code before the start of the Main () function (I hope that they will list all that I know and a couple of unknowns :))?

    And now the puzzle:

    Here is a small code that displays two characters (by the way, check yourself: in what order will they be displayed?). An underscore "_" must be displayed between these characters.
    Of course, the task would be trivial without restrictions. And they are as follows:

    • do not define another Main method
    • do not use the Console identifier as the name of the class, property, field, etc. (thanks to irc-user Gopneg)
    • do not change the code of all existing methods: App.Main, XX, YY
    • do not change the definition of the Xy field
    • do not add new fields to class X


    using System;

    class Y
    {
      public Y()
      {
        Console.Write("0");
      }
    }

    class X
    {
      public X()
      {
        Console.Write("o");
      }
      Y y = new Y();
    }

    class App
    {
      static void Main()
      {
        X x = new X();
      }
    }

    * This source code was highlighted with Source Code Highlighter.


    Good luck!

    PS
    so, we have two winners: mace was the first and guessed the author’s decision: http://habrahabr.ru/blogs/net/77039/#comment_2241079
    and the bobermaniac habrayuzer suggested a way to elegantly bypass my restriction on redefining Console: http: // habrahabr .ru / blogs / net / 77039 / # comment_2241109

    By the way, the question of calling the code before Main is still relevant! So far, the most obvious has been proposed - the static constructor of the class.

    PPS
    Suddenly, another solution was proposed by SHSE : http://habrahabr.ru/blogs/net/77039/#comment_2243183 , and it is based on completely different mechanisms, but it works great! )

    Also popular now: