Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write syntax like - ()()?

Tags:

c#

I read in an ebook somewhere (which I'm desperate to find again), that, by using delegates, it is possible to write code which has syntax as follows:

 ()(); // where delegate precedes this.

Can anyone provide any details how this would be possible/in what situation this would occur?

like image 801
GurdeepS Avatar asked Apr 13 '10 20:04

GurdeepS


5 Answers

You can do slightly better than the examples given so far, in fact... you can extend it arbitrarily:

class Test
{
    delegate Hofstadter Hofstadter();

    static void Main()
    {
        // Unfortunately I'm clearly not as smart as the real thing
        Hofstadter douglas = () => null;

        douglas()()()()()()();
    }
}

And here's another horrible alternative, for extra ASCII art:

class Test
{
    delegate __ ___();
    delegate ___ __(___ _);

    static void Main()
    {
        ___ _ = () => null;

        _ ()((_))();
    }
}

Please never ever, ever do this.

EDIT: One last one - although it's as much about just replacing things with underscores as anything else, and reusing names wherever possible:

class Test
{
    delegate void _();
    delegate __<_> ___<_>();
    delegate ___<_> __<_>(___<_> ____);

    static ___<_> ____<_>(___<_> ____) { return ____; }
    static __<_> ____<_>() { return ____<_>; }

    static void Main()
    {
        ((__<_>)____)(____<_>)();
    }
}
like image 197
2 revs Avatar answered Nov 18 '22 23:11

2 revs


Here's a sample program that demonstrates this:

using System;

class Program
{
    static Action GetMethod()
    {
        return () => Console.WriteLine("Executing");
    }
    static void Main()
    {
        GetMethod()();
        Console.ReadKey();
    }
}

That being said, I wouldn't ever do this in production code. It's very unexpected.


Edit: Just in case you want to see something even uglier... [especially the "()()[()=>{}]()"]:

using System;

class Program
{
    static void Main()
    {
        (new Program()).Confusion();
        Console.ReadKey();
    }

    public Action this[Action index]
    {
        get {
            return () => Console.WriteLine("Executing");
        }
    }

    Func<Program> GetInstance()
    {
        return () => this;
    }

    void Confusion()
    {
        // This is particularly ugly...
        GetInstance()()[()=>{}]();
    }
}
like image 25
Reed Copsey Avatar answered Nov 18 '22 22:11

Reed Copsey


You just need a little self referencing, and you can call it as many times as you like:

delegate Foo Foo();

class Program {
    static void Main(string[] args) {
        Foo f = null;
        f = () => f;
        // Add more "()" as you feel like...
        f()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()();
    }
}
like image 37
Richard Avatar answered Nov 18 '22 23:11

Richard


static void Foo()
{
    Console.WriteLine("Hello World!");
}

static Action Bar()
{
    return new Action(Foo);
}

static void Main()
{
    Func<Action> func = new Func<Action>(Bar);
    func()();

    Bar()();
}

prints

Hello World!
Hello World!

This works, because func() and Bar() return an Action delegate which can be invoked using normal method invocation syntax.

like image 38
dtb Avatar answered Nov 18 '22 23:11

dtb


Something like:

delegate void FunctionA();
delegate FunctionA FunctionB();

void TestA() { }
FunctionA TestB() { return TestA; }

void Main()
{
   TestB()();
}
like image 2
csharptest.net Avatar answered Nov 19 '22 00:11

csharptest.net