tags:

views:

567

answers:

3

In C# 3.0 we got a lot of cool tools at our disposal with anonymous types, lambda expressions etc. This sometimes leads to some... interesting looking code that contains a smaller sequence of special characters.

Although it's not Friday yet, I thought I'd ask: What's the longest, most obscure sequence of non-character C# code you can write?

Some rules:

  • The code must compile.
  • With non-character are meant any character that has special meaning in C#, such as <>{}[]&| etc. Unicode characters like æ, ø or å means nothing to C#, so don't count.
  • No repeating patterns are allowed - e.g. a lot of nested statements with closing curly braces: }}}}} are not allowed, and neither are variations thereof: })})})
  • Whitespace is allowed, but doesn't count
  • Only characters in code count. That is: characters within strings don't.

To get you started, here's an example of what I mean:

this.Action = () => { };

This compiles if the enclosing type has an Action property defined like this:

public Action Action { get; set; }

Not counting whitespace, this statement has 8 special characters in a sequence, but I'm sure you can do much better.

I will accept the valid answer that has the longest sequence. In case of ties, I will pick the one with most votes.

+1  A: 

This isn't very long, but it's an obscure way of doing a backwards for loop:

for (int i = myArray.Length; i --> 0; )
{    
    //do something
}

And without any of this fancy-shmancy C# 3.0 stuff!

MusiGenesis
Anyone doing this should be banned from programming. Forever.
DrJokepu
I'm not advocating this - I'm just answering the question. :)
MusiGenesis
Why is it so bad (other than unexpected whitespace)? IMO it is the cleanest way to achieve a backwards for loop (even works with uint).
UncleBens
@UncleBens: it's bad because it elicits a universal WTF? reaction, and it requires extra mental energy to figure out what it's trying to do. It is pretty, though, but I'd rather see some syntax like "foreach (int i in myArray) backwards {}".
MusiGenesis
@UncleBens: This is wrong on many levels. First, backwards loops are uncool (think about the CPU cache). Second, this falls into the realm of practical jokes I call "fun with operators" and it's really, really not a very nice thing to do. Fortunately modern IDEs will reformat it properly automatically.
DrJokepu
@DrJokepu: I can't believe you're harshing on my backwards loop, and leaving "_ = (_, __) => _[(_[__--]+=__)]" untouched. :)
MusiGenesis
+11  A: 

I'm cheating since I use underscores, but it's a very random try.

Given:

private Func<int[], int, int> _;

You could write things like:

_ = (__, _) => (__[_+(__[-_]+=_)]&=-(_+_|(_^=-_) *__[--_]|_));

Well, it's not that difficult to make that longer. Once again, I'm cheating.

Romain Verdier
Very obscure :) You have repeating patterns there (repeating underscores), but even so, the longest non-repeating sequence I could find contained 10 characters. I have no idea what it does, though...
Mark Seemann
And they want to ban *me* for my backwards for loop. Yeesh.
MusiGenesis
@Mark, _'s don't count though, right? So the longest sequence is 4 or so. If _'s count, you can alter my answer to y = @_ => () => { @_++; }; for a total of 13 characters.
Jason
+1  A: 

10 Characters:

Func<int,Action> y = @new => () => { ++@new; };
Jason
Pretty nice, but you have two repeating + signs there, so it only counts as 8.
Mark Seemann
You could replace @new with @_, I think.
yodaj007
@yoda: _ is not a special C# character. @Mark, it's 9 then, not 8: =, >, (, ), =, >, {, +, @.
Jason
True. I had http://stackoverflow.com/questions/1367675/most-obscure-sequence-of-non-character-c-code-that-compiles/1367761#1367761 that post in mind.
yodaj007
Good point. _'s shouldn't count there either.
Jason