6

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);
    }
}
4

3 に答える 3

13

中かっこのペアが欠落していると思います-代わりに{10}読むべき{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

    }
}
于 2009-11-19T12:46:22.237 に答える
6

オリジナルは使えますか?

s={0}{1}{0}
于 2009-11-19T12:46:18.650 に答える
5

元はこんな感じだったと思います。

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);
  }
}

つまり、{0}{10}を に変更するだけ{0}{1}{0}です。

フォーマット文字列の{0}は、文字列の前後に引用符を挿入するために使用されます。

于 2009-11-19T12:48:47.740 に答える