Can I use params in Action or Func delegates?

Homam picture Homam · Oct 30, 2010 · Viewed 27.5k times · Source

When I'm trying to use params in an Action delegate...

private Action<string, params object[]> WriteToLogCallBack;

I received this design time error:

Invalid token 'params' in class, struct, or interface member declaration

Any help!

Answer

CodesInChaos picture CodesInChaos · Oct 30, 2010

How about this workaround?

private Action<string, object[]> writeToLogCallBack;
public void WriteToLogCallBack(string s, params object[] args)
{
  if(writeToLogCallBack!=null)
    writeToLogCallBack(s,args);
}

Or you could define your own delegate type:

delegate void LogAction(string s, params object[] args);