views:

49

answers:

3

Hi All,

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

+2  A: 

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.

Rich
Hi Rich,I get the following:Error 2 The type or namespace name 'T1' could not be found (are you missing a using directive or an assembly reference?) D:\Eyal\c.sharp\tests\ippc\ippcRemoteTest\ippc_server.cs 174 89 ippcRemoteTest
Eyalk
@Eyalk: Sorry about that, see my edit.
Rich
+2  A: 

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;
}
Darin Dimitrov
+1  A: 

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)
Jon Skeet
Thanks Jon for the explanation and advise. I'll try to be more aware to .NET naming conventions.
Eyalk
BTW - why did you add Func<T1, T2, int> instead of Func<T1, T2> ?
Eyalk
@Eyalk: Because your delegate type returns int, not T2.
Jon Skeet