C# Quine Problem

牧云@^-^@ 提交于 2019-12-10 04:24:57

问题


I am trying to understand how this piece of self-replicating code works (found here), but the problem is I can't get it to run as-is:

class c {
    static void Main(){

        string s = "class c{{static void Main(){{string s={0}{10};System.Console.Write(s,(char)34,s);}}}}";

        System.Console.Write(s,(char)34,s); //<<-- exception on this line

    }
}

It's throwing an exception on writeline: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Can someone help - in particular about the formatting option {0}{10}?

I got it working like this (see below) but it's longer than the original - I am curious how the original could have worked as-is in the 1st place:

class c {
    static void Main(){

        string s = "class c{{static void Main(){{string s={0}{1}{2};System.Console.Write(s,(char)34,s,(char)34);}}}}";

        System.Console.Write(s,(char)34,s,(char)34);
    }
}

回答1:


I think there is a pair of braces missing - instead of {10} it should read {1}{0}.

class c {
    static void Main(){

        string s = "class c{{static void Main(){{string s={0}{1}{0};System.Console.Write(s,(char)34,s);}}}}";

        System.Console.Write(s,(char)34,s); //<<-- exception on this line

    }
}



回答2:


Could the original work with?

s={0}{1}{0}



回答3:


I believe that the original was supposed to look like this:

class c {
  static void Main() {
    string s = "class c{{static void Main(){{string s={0}{1}{0};System.Console.Write(s,(char)34,s);}}}}";
    System.Console.Write(s, (char)34, s);
  }
}

I.e. the {0}{10} should just be changed to {0}{1}{0}.

The {0} in the format string is used to put the quotation marks before and after the string.



来源:https://stackoverflow.com/questions/1762984/c-sharp-quine-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!