Implicit cast to Func or Action delegates?

c#, casting, delegates, func, implicit

Solution

No, C# does not allow that as part of the language. Underneath the covers, the CLI uses the `call` and `callvirt` instructions to invoke a method or, indirectly, a delegate.

So, unlike Python where you can make an instance of a class `callable` by declaring a `def __call__` method, C# has no similar feature.

Problem

C# allows you to define an implicit cast to a delegate type: ``` class myclass { public static implicit operator Func<String, int>(myclass x) { return s => 5; } public static implicit operator myclass(Func<String, int> f) { return new myclass(); } } ``` But unfortunately, we can't use this to make an object look like a function: ``` var xx = new myclass(); int j = xx("foo"); // error Action<Func<String, int>> foo = arg => { }; foo(xx); // ok ``` Is there a nice way to make an object of one's own class accept function-style parameters (arguments) directly on of its base instance? Kind of like an indexer, but with parentheses instead of square brackets?

Original source