Reputation: 465
I am new to c#. I am trying to declare a delegate function which takes 2 generics inputs. I am having problems compile it. can anyone tell me what is the problem here.
delegate int ippcFuncPtr<in T1, in T2>(T1 param, T2 returnval);
static int ippcServerRegisterProcedure(int handle,
string procedureName, ippcFuncPtr<in T1, in T2> procedure)
{
return 0;
}
Thanks, Eyal
Upvotes: 1
Views: 111
Reputation: 3101
You must explicitly declare the type parameters on the method, like so:
delegate int ippcFuncPtr<in T1, in T2>(T1 param, T2 returnval);
static int ippcServerRegisterProcedure<T1, T2>(int handle, string procedureName, ippcFuncPtr<T1, T2> procedure) {
return 0;
}
See Generic Delegates (C# Programming Guide) and Generic Methods (C# Programming Guide) on the MSDN for more information.
Upvotes: 2
Reputation: 1503839
You don't need to redeclare the contravariance in the parameter, but you do need to give the method type parameters, unless it's in a generic type:
static int ippcServerRegisterProcedure<T1, T2>(int handle, string procedureName,
ippcFuncPtr<T1, T2> procedure)
I'd also strongly recommend that you follow .NET naming conventions - and use the standard delegates where possible... so in this case:
static int RegisterIpccServerProcedure<T1, T2>(int handle, string procedureName,
Func<T1, T2, int> procedure)
Upvotes: 1
Reputation: 1039498
The problem is that you haven't defined the T1 and T2 generic arguments on the ippcServerRegisterProcedure
function. Try like this:
static int ippcServerRegisterProcedure<in T1, in T2>(
int handle,
string procedureName,
ippcFuncPtr<in T1, in T2> procedure
)
{
return 0;
}
Upvotes: 2