Let's say I have an interface IMyInterface<T>
that simply describes one function:
public interface IMyInterface<T>
{
T MyFunction(T item);
}
I could just about replace this with Func<T, T>
, but I want the interface for semantic reasons. Can I define an implicit conversion between that interface and Func<T,T>
such that I could pass an anonymous delegate or lambda as an argument to a function that accepts this interface as a parameter, just like if I had used Func<T,T>
instead?
To demonstrate, using the interface declared above I want a function like this:
public T TestFunction<T>(IMyInterface myInterface, T value)
{
return myInterface.MyFunction(value);
}
That I can call like this:
TestFunction<string>( x => return x + " world", "hello");
And the result would be "hello world".