c# - Passing an extension method to a method expecting a delegate. How does this work? -


So at work I was using an API that we did not write, and one of the methods took a representative . For one reason or another, this idea came to me that I have an extension method that matches that signature, so I was thinking that this would work. I was sure that this is not my surprise, but it did. Allow me to display:

I have these classes:

  Public interface IMyInterface {} Public class MyClass: IMyInterface {} Public Fixed class extension {public static string FuncMethod (this IMyInterface imy, int x) {return x.ToString (); }}  

We now say that I have a method signature that looks like this:

  Private static zero method (function & lt; int , String & gt; func) {}  

Now my expansion method (looks like this) matches that signature, but we all know that the extension methods are only smoke and mirrors , So it actually still matches that signature, I can safely do this:

  var example = New MyClass (); Method (instance.FuncMethod);  

My question is, how does this work? To make this acceptable, the compiler prepares for me an example of the actual signature of the extension method IMyInterface , but Func is not such that behind me the screen is behind Happening?

Examples of methods such as taking hidden this parameter are applied .

When you refer to an expansion method, for example, the hidden this parameter method is first sent as a normal parameter.


Comments