News for hackers. Stuff that matters.
Re:Objects vs closures by noblethrasher on Wednesday December 08, 2010 @01:18PM (#1983952)
A closure is also a poor man's class (sort of).

I've often wished that C# let me implement interfaces with anonymous classes. Since it doesn't, I get around it by creating AdHoc classes for interfaces that I use a lot.

For instance, if I have an interface Foo defined as:

    interface Foo
    {
        string Bar(int x);
    }
then I also create a corresponding class called AdHocFoo defined like so:

    class AdHocFoo : Foo
    {
        Func<int, string> _bar;

        public AdHocFoo(Func<int, string> bar)
        {
            _bar = bar;
        }

        public string Bar(int x)
        {
            return _bar(x);
        }

    }
I've written a little program that creates these AdHoc class definitions from interfaces.

There are no comments on this comment yet.