What is the longest legal statement block you can make with only C# keywords?

后端 未结 8 1384
粉色の甜心
粉色の甜心 2020-12-28 16:39

I was writing some code in C#, and I found myself writing:

return new MyClass(...

when I noticed that both the return and the

相关标签:
8条回答
  • 2020-12-28 17:18

    Here is another case that can be as long as you wish:

    do do do do do do do do do do do do do do do // ...
    while(x) while(x) while(x) while(x) while(x) // ...
    

    With contextual keywords you can also have

    await await await await await await await // ...
    
    0 讨论(0)
  • 2020-12-28 17:20

    Can I cheat?

    internal protected static volatile StringBuilder @string = 
      new StringBuilder(int.Parse("12"));
    

    Using the fact that I can use a keyword or other reserved term as a variable name if I prepend it with an @ - comes in at 9 if you allow the duplication of StringBuilder.

    0 讨论(0)
  • 2020-12-28 17:23

    I guess it's infinite:

    return null as string as string as string as string as string....
    
    0 讨论(0)
  • 2020-12-28 17:25

    For 6:

    new protected internal unsafe virtual decimal Foo() {...}
    

    Edit for 7:

    new protected internal unsafe virtual extern decimal Foo();
    

    If we allow brackets and braces...

    (edited the "lock", "new object()", "as" and "string" were contributed by others; see comments)

    decimal Bar() {
        lock (new object() as string) {
            if (true) {
                checked {
                    unsafe {
                        try {
                            do {
                                return default(decimal);
                                unchecked {break;}
                                continue;
                            } while (false);
                        }
                        catch { throw; }
                        finally { }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:31

    One more variant with method definition (found by my colleague):

    protected internal override sealed unsafe async void await() { ... }
    

    Makes 8 keywords in a row. Uses the fact that await is a contextual keyword, so it can be reused for method name.

    0 讨论(0)
  • 2020-12-28 17:32
    internal protected static volatile string foo = "bar";
    

    That's 5.

    0 讨论(0)
提交回复
热议问题