0

I´ve read 'C# anonymously implement interface (or abstract class)' thread for implementing an interface anonymously. But I wondered if this is also possible using .NET 2.0 (NO LINQ) using delegates or any similar approach. I know from JAVA the following possible:

MyClass m = MyMethod(new MyInterface() {
    @override
    public void doSomething() {...}
}

(I hope I remember well, is a time ago that I used JAVA, but I suppose it was something similar). This might be helpful whenever a method needs an instance of an interface and is called only once so there is no need to create a new class for this single approach.

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111

2 Answers2

1

.NET 2.0 also supported anonymous delegates, it's just that the syntax was a bit more verbose compared to lambdas, and type inference didn't work. And there were no extension methods in C# 2.0 (although you were able to use C# 3.0 and compile against .NET 2.0), which are the basis of LINQ and being able to operate on interfaces.

Compare:

  • .NET 2.0: delegate(int i) { return (i < 5); }
  • .NET 3.5: i => i < 5

.NET 2.0 also lacks common generic delegate signatures (Func and Action), but you can also easily define them yourself (for all combinations of parameters you like):

public delegate void Action<T>(T item);
public delegate Tresult Func<T, Tresult>(T item);

So, whatever approach your linked answer used to mimic anonymous interfaces can be represented using .NET 2.0 delegates, at the expense of added verbosity. Making you ask yourself: "is this really that shorter to write?"

[Update]

If your interface is a single method interface, like:

interface IFoo
{
    string Bar(int value);
}

class SomeOtherClass
{
    void DoSomething(IFoo foo);
}

then you might get rid of it entirely and simply use a delegate instead:

class SomeOtherClass
{
    void DoSomething(Func<int, string> bar);
}

new SomeOtherClass().DoSomething(delegate(int i) { return i.ToString(); });

If you have an interface with many methods that you want to be able to implement inline in many different places, you can use something like this:

interface IFoo
{
    string GetSomething();
    void DoSomething(int value);
}

// conditional compile, only if .NET 2.0
#if NET_2_0
public delegate void Action<T>(T item);
public delegate Tresult Func<Tresult>();
#endif

class DelegatedFoo : IFoo
{
    private readonly Func<string> _get;
    private readonly Action<int> _do;

    public DelegatedFoo(Func<string> getStuff, Action<int> doStuff)
    {
        _get = getStuff;
        _do = doStuff;
    }

    #region IFoo members simply invoke private delegates

    public string GetSomething()
    { return _get(); }

    public void DoSomething(int value)
    { _do(value); }

    #endregion
}

Which would allow you to pass delegates to the DelegatedFoo class inline:

var delegated = new DelegatedFoo(
    delegate() { return ""; }, // string GetSomething()
    delegate(int i) { }        // void DoSomething(int)
);

Using .NET 4 the C# 4.0 syntax it would look a bit cleaner due to syntactic sweetness of lambdas and named parameters:

var delegated = new DelegatedFoo(
    getStuff: () => "",
    doStuff:  i => { }
);
vgru
  • 49,838
  • 16
  • 120
  • 201
  • Well, I do not actually look for a shorter, but for an inline-way doing this. This is because creating types for using them only once in a quite specific context is annoying for me. – MakePeaceGreatAgain Jul 24 '14 at 10:44
  • Well, as stated in the other answer, it's not possible in .NET (even in current .NET 4.5). If your interface is a single-method one, then it's functionally equivalent to a delegate, and you *can* create it anonymously. If not, you either have to use a duck typing/proxying framework (Castle, LinFu and other) (which, on the other hand, still doesn't really allow you to do it inline), or, you can create a class which implements your interface but actually only invokes delegates set during instantiation. – vgru Jul 24 '14 at 11:15
  • Yeah, my interface is single-method one, so how to make it a delegate (without changing the interface itself ofc)? – MakePeaceGreatAgain Jul 24 '14 at 11:21
  • @HimBromBeere: added a couple of examples. – vgru Jul 24 '14 at 11:28
  • Wow, I want .NET 4.0... this expression reminds me on Javascript: getStuff = function(...) {...} – MakePeaceGreatAgain Jul 24 '14 at 11:35
  • @HimBromBeere: actually, one correction: it's C# 4.0 (comes with VS2010) to be precise. You can compile the same thing against .NET 2.0 if you want to stay at that version (just tried it inside VS2012 also). But I would also consider moving forward; [.NET 3.5 was released in 2007, that's a century in IT years. :)](http://en.wikipedia.org/wiki/.NET_Framework_3.5#.NET_Framework_3.5). And if you are a fan of "inline", LINQ is really great once you get the hang of it - you can remove most of the loops in your code and get a step closer to functional programming with these features. – vgru Jul 24 '14 at 11:45
0

I know that this may not be exactly what you are hoping for, but if you absolutely have to do it, you can use any of the mocking frameworks available to request an object which implements the interface and then add implementations for the methods. This is a standard practice in TDD.

Also, you can simply use anonymous delegates to achieve most of your needs as per John Skeet's advice in the question your mention.

Savvas Kleanthous
  • 2,695
  • 17
  • 18