views:

221

answers:

3

How, in C#, do I have a Func parameter representing a method with this signature?

XmlNode createSection(XmlDocument doc, params XmlNode[] childNodes)

I tried having a parameter of type Func<XmlDocument, params XmlNode[], XmlNode> but, ooh, ReSharper/Visual Studio 2008 go crazy highlighting that in red.

Update: okay, Googling for 'c# params func' produced no results, but 'c# params delegate' led me to this question. Following Jon Skeet's answer there, it looks like maybe I could create a delegate, say Foo, and then instead of having a parameter to my method of type Func<XmlDocument, params XmlNode[], XmlNode>, I take a parameter of type Foo.

A: 

I suggest 'params' is not allowed in this case.

philipproplesch
+2  A: 

You can't have params in a delegate declaration. You can, however, take a single array, which would work for what you need: Func<XmlDocument, XmlNode[], XmlNode>.

Stephen Cleary
+1  A: 

Jon Skeet's answer to this other question led me to try the following, which works:

protected delegate XmlNode CreateSection(XmlDocument doc,
    params XmlNode[] childNodes);

protected static void createOrUpdateSettingTree(XmlNode rootNode,
    XmlDocument doc, CreateSection createSection) { ... }
Sarah Vessels