Let's say I have the following method:
int ReturnNumber(int number) { return number; }
Now let's say that I also have the following two methods: regular method:
void Print(Func<int, int> function)
and extension method:
static void Print(this Func<int, int> function)
I can name the first one like this:
Print(ReturnNumber); // Regular method call, seems to implicitly convert ReturnNumber to Func<int, int>
but I can not do this with the latter:
ReturnNumber.Print(); // Extension method call, does not seem to do the implicit conversion -- results in compiler error
although I can do this:
((Func<int, int>)ReturnNumber).Print(); // I perform the conversion explicitly
I assume that there is some βmagicβ that occurs when you pass a method as an argument to another method and that the compiler can therefore assume that it should try to convert ReturnNumber to Func<int, int> , while the compiler does nothing of the kind for extension methods. It's right? My question can be summarized as: why can't you call the method extension method, while you can call the extension method on the delegate instance? Is this due to the fact that the compiler does not consider methods as objects, but only considers delegates as objects?
source share