views:

602

answers:

4

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 :-)

+1  A: 
internal protected static volatile string foo = "bar";

That's 5.

Razzie
+23  A: 

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 { }
                }
            }
        }
    }
}
Marc Gravell
Awesome !
scraimer
if you are counting characters:new protected internal unsafe virtual extern string Foo();
Andrew Robinson
decimal would be better ;-p
Marc Gravell
No using statement? :P
meandmycode
you might add: lock(new object()) {} somewhere ;)
devio
string / decimal... got me. it is early here.
Andrew Robinson
could change the return type to decimal, and then 'return (decimal) new double(); :p
Svish
If it wasn't for 'as' not working with value types: return (decimal) new float() as double; hehe
Svish
as long as you're allowed to use braces, you could replace new object() as string; with: new bool[] { null as string is int }
Niki
also, you could replace default(decimal) with (object)typeof(default(decimal)) for two cheap extra keywords.
Niki
Selected Answer - because it's the most creative!
scraimer
A: 

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.

Zhaph - Ben Duguid
StringBuilder isn't a keyword, nor is Parse
Marc Gravell
I never knew about the @ thing! But I don't think StringBuilder counts as a keyword. It's just a class, no?
scraimer
Fair enough. Also I wasn't count Parse, but was double counting StringBuilder.
Zhaph - Ben Duguid
+19  A: 

I guess it's infinite:

return null as string as string as string as string as string....
Niki
We have a winner!
Daniel Earwicker
Pah; I'd prefer *different* terms... but I can't argue the truth of it...
Marc Gravell
Would this affect the compiler?
Sander Versluys
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
then you'd repeat 'is' and 'as' pretty many times though...
Svish
Well, Marc's example is repeating '{' and '}' pretty many times, and those are not even keywords ;-)
Niki
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
This is turning into Just a Minute. http://www.bbc.co.uk/radio4/comedy/justaminute.shtml
Daniel Earwicker