21

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

return new MyClass(...

when I noticed that both the return and the new were both C# keywords. So I wondered what is the longest legal sequence of keywords in C#. All I could think of is:

internal static override void MyFunc(...

Where internal static override void are all keywords. Can you think of a longer sequence of keywords?

Note: There's really no point to the question. I'm just hoping to pour more some fun on the fire :-)

6
  • 1
    Isn't this more like "anti golf"? Mar 23, 2009 at 11:17
  • 1
    hmm yes, static override - well to be honest, he didn't say the code had to compile... ;-)
    – Razzie
    Mar 23, 2009 at 11:20
  • Oh, but I wish it did compile! I'm merely lacking coffee and sleep :-) Mar 23, 2009 at 11:23
  • 1
    i must say, cool question :-) fun c# programming trivia! Mar 23, 2009 at 11:46
  • Well, the 7 is all distinct terms... no braces... I assume you'll let me keep white-space? Mar 23, 2009 at 12:05

8 Answers 8

49

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 { }
                }
            }
        }
    }
}
14
  • if you are counting characters: new protected internal unsafe virtual extern string Foo();
    – andleer
    Mar 23, 2009 at 11:31
  • you might add: lock(new object()) {} somewhere ;)
    – devio
    Mar 23, 2009 at 11:47
  • 5
    also, you could replace default(decimal) with (object)typeof(default(decimal)) for two cheap extra keywords.
    – Niki
    Mar 23, 2009 at 12:36
  • 1
    throw null; adds another keyword. May 19, 2011 at 8:51
  • 3
    Add async in method declaration for one more keyword. Dec 26, 2013 at 7:35
36

I guess it's infinite:

return null as string as string as string as string as string....
5
  • Would this affect the compiler? Mar 23, 2009 at 11:47
  • 2
    The number of different keywords is limited, so you can't produce an infinite chain without repetitions. But you can produce pretty long chains: return true is bool as object is int as string is byte as double...
    – Niki
    Mar 23, 2009 at 11:53
  • Well, Marc's example is repeating '{' and '}' pretty many times, and those are not even keywords ;-)
    – Niki
    Mar 23, 2009 at 11:55
  • true I guess, but should we count spaces as repeated non-keyword too then? :p just kidding, hehe. I found your answer the most clever one. but I find Marc's the longest without repeating (even though the brackets are repeated).
    – Svish
    Mar 23, 2009 at 11:59
  • This is turning into Just a Minute. bbc.co.uk/radio4/comedy/justaminute.shtml Mar 23, 2009 at 12:05
3

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 // ...
3

10 with extern partial hacks

abstract partial class A
{
    private protected virtual extern unsafe ref readonly delegate*<int> Test();
}

partial class B : A
{
    private protected sealed override unsafe partial ref readonly delegate*<int> Test();

    // 1        2       3       4       5      6       7     8     9        10
    private protected sealed override extern unsafe partial ref readonly delegate*<int> Test();
}
1
internal protected static volatile string foo = "bar";

That's 5.

0

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.

2
  • in that context await is not a keyword
    – koryakinp
    May 7, 2020 at 19:34
  • @koryakinp: nevertheless await is a contextual keyword per se. The original post didn't require the identifier be a keyword exactly in the context as presented in the answer.
    – Vlad
    May 7, 2020 at 19:54
0
public abstract class Base {
    protected internal abstract ref readonly int MyMethod();
}

public class Sub : Base {
    extern sealed protected internal override unsafe ref readonly int MyMethod();
}

Managed to get 9 into a method declaration. Not sure if it's possible to get more in a method declaration

-2

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.

3
  • 5
    StringBuilder isn't a keyword, nor is Parse Mar 23, 2009 at 11:26
  • I never knew about the @ thing! But I don't think StringBuilder counts as a keyword. It's just a class, no? Mar 23, 2009 at 11:26
  • Fair enough. Also I wasn't count Parse, but was double counting StringBuilder. Mar 23, 2009 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.