Okay, so after reading into creating custom function delegates after Func<> didnt accept a ref keyword inside the type declaration, I ended up with this delegate:
private delegate TResult RefFunc<T, out TResult>(ref T arg);
inside my little test project. I also declared these two functions:
private static string FirstFunction(string arg) {
return "";
}
private static string SecondFunction(string arg) {
return "";
}
(Since I discovered the problem, this truly is the only function content!)
that adhere to the above delegate and are passed as parameters to this function:
private static void SampleFunction(RefFunc<String, String> argOne, RefFunc<String, String> argTwo) { ... }
Like so (simplified):
private static void Main(string[] args) {
SampleFunction(FirstFunction, SecondFunction);
}
That function call wouldn't work out because "Conversion of 'method group' to '<class>.RefFunc<string, string>' isn't possible
", which makes sense - I'm directly passing a function without turning it into a delegate first - although I'm pretty sure I've seen that same syntax working with the Action<> Delegate somewhere. Anyways, I then modified my caller code in the Main() function in many ways while looking into the issue, however none of the below approaches resolved the issue.
SampleFunction(new RefFunc<String, String>(FirstFunction), ...); // Causes "No overload for FirstFunction matches delegate RefFunc<string, string>" at the first function parameter
SampleFunction(RefFunc FirstFunction, ...); // Was worth a try
RefFunc handler = FirstFunction; SampleFunction(handler, ...); // As described in docs. Causes "The usage of type "RefFunc<T, TResult>" (generic) requires 2 type arguments" at keyword RefFunc
RefFunc<String, String> handler = FirstFunction; SampleFunction(handler, ...); // Didn't help it. Causes the same error as with the first attempt
// and a few others
Docs
I've finally decided to turn to stackoverflow. Since my functions clearly adhere to the delegate I created, I can't really understand why C# believes they do not. I appreciate any help!
Please note that while I'm only showing code relevant to the error caused, the project is very small, so I'm sure the problem doesn't lie elsewhere
Since my functions clearly adhere to the delegate I created
Unfortunately, neither of your FirstFunction
and SecondFunction
is of RefFunc
type, just because the T
is passed by ref
in the delegate's defintion and in the same time you lack ref
in your actual functions.
Either you modify both functions
public class Program
{
private delegate TResult RefFunc<T, out TResult>(ref T arg);
private static string FirstFunction(ref string arg) {
return "";
}
private static string SecondFunction(ref string arg) {
return "";
}
private static void SampleFunction(
RefFunc<String, String> argOne, RefFunc<String, String> argTwo)
{
}
public static void Main()
{
SampleFunction(FirstFunction, SecondFunction);
}
}
or you drop ref
private delegate TResult RefFunc<T, out TResult>(T arg);
which now is a correct type for
private static string SecondFunction(string arg) {
return "";
}