C# delegate for two methods with different parameters

后端 未结 3 1541
情话喂你
情话喂你 2020-12-14 19:02

I am using the following methods:

public void M1(Int32 a)
{
  // acquire MyMutex
  DoSomething(a);
  // release MyMutex
}

and



        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 19:47

    Yes, it's possible to combine generics with delegates.

    public delegate void Action(T x);
    public delegate void Action(T x, U y);
    
    public void UsingMutex(Action x, T t) {
        // acquire mutex...
        x(t);
        // release mutex...
    }
    public void UsingMutex(Action x, T t, U u) {
        // acquire mutex...
        x(t, u);
        // release mutex...
    }
    

    But you still have to handle different number of parameters using overloads.

提交回复
热议问题