简体   繁体   中英

C# params without an inline array cause an error “Named argument specifications must appear after all fixed arguments have been specified”

I have created a method called "tag" that returns an HtmlTag object and get params of type "HtmlTag" (See below).

I'm trying to pass the params without an inline array but I get an error: "Named argument specifications must appear after all fixed arguments have been specified".

The error resolved only by inserting the params in an inline array (which I really don't want to).

Can't I pass the params without an array?

protected HtmlTag tag(string tagName, string id = null, string classes = null, 
     Dictionary<string, object> attributes = null, Dictionary<string, object> data = null, 
     string text = null, params HtmlTag[] content)
{yada yada...}

See below how I call the method from above:

tag("form", "", attributes: ObjList("...."), content: 
                    tag("input", "token", attributes: ObjList("..." + token + "...")),
                    tag("label", "...", attributes: ObjList("..."), text: "..."),
                    tag("...", "...", attributes: ObjList("...")));

I have no errors when I insert the "content" params value inside an inline array of HtmlTag (see below):

tag("form", "", attributes: ObjList("...."), content: new HtmlTag[] {
                    tag("input", "token", attributes: ObjList("..." + token + "...")),
                    tag("label", "...", attributes: ObjList("..."), text: "..."),
                    tag("...", "...", attributes: ObjList("..."))});

Thanks to Nyerguds and Jcl I'm using overloaded method as an answer. Seems like it is the only way to go (other than inline array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM