More Dynamic Than Thou

time to read 6 min | 1017 words

Well, that didn't take very long. In a couple of hours, I have four ways to solve my challange. I guess this means that C# is getting near C++ in expressiveness, I guess.

Omer van Kloeten got very near to what I had in mind, but I am afraid that it is not dynamic enough for me. So, without further ado, I present to you my Foo:

class Foo

{

    public Action<string> DoWork = delegate { };

}

 

static void Main (string[] args)

{

    Foo foo = new Foo();

    foo.DoWork("Foo");

    foo.DoWork = Console.WriteLine;

    foo.DoWork("Bar");

}

Now the challange is to consider when this would be useful...

Hint: A good place to search for reasons to do this would be Ruby / Python, where you can do this stuff

Note: It is trivial to make this code work in 1.1 as well.