views:

45

answers:

3

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!

+2  A: 

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);
CodeInChaos
@CodeInChaos: Have you tried it?
Homam
`params` in type parameters or type arguments are not supported. I think you meant it without the first `params`.
Jordão
Apart from the copy paste mistake(forgot to remove params in the delegate declaration) it works as expected.
CodeInChaos
Yeah, now it works.
Jordão
A: 

You can use params in the actual declaration of a delegate, but not in type of one. The generic parameters to an Action are only types, not the actual arguments to be passed when invoking the delegate. params is not a type, it is a keyword.

CrazyJugglerDrummer
+1  A: 

Variadic type parameters are not possible in C#.

That's why there're many declarations for Action<...>, Func<...>, and Tuple<...>, for example. It would be an interesting feature, though. C++0x has them.

Jordão
Not sure how well it would work with generics (as opposed to templates).
CodeInChaos