Eyalk
Eyalk

Reputation: 465

problem with generics delegate

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

Answers (3)

Rich
Rich

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

Jon Skeet
Jon Skeet

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

Darin Dimitrov
Darin Dimitrov

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

Related Questions