This compiles but the second method is flagged as “Function is recursive on all paths.” and calling it results in a StackOverflowException. Intellisense (w/ ReSharper) supplied Invoke as a property.
public class Class1
{
public void MyMethod(string value)
{
Console.WriteLine(value);
}
public void MyMethod(Func<string> getValue)
{
MyMethod(getValue.Invoke);
}
}
Changing it to this works as expected:
public void MyMethod(Func<string> getValue)
{
MyMethod(getValue.Invoke());
}
What’s going on here? Is this just Intellisense weirdness or is there actually an Invoke property?
It’s the
Invokemethod – but being converted via method group conversion… which is then recursing. See if this makes it any clearer – it’s the equivalent code:(I’ll assume for the moment that you’re familiar with method group conversion as one way of obtaining a delegate instance. Let me know if you’re not and I’ll go into more details.)