简体   繁体   English

同一类型的多个可选参数的构造函数选择

[英]Constructor selection for multiple optional parameters of the same type

In the code below, CreateNode calls the first constructor, as explained here (thanks for the link).在下面的代码中, CreateNode调用第一个构造函数,如此所述(感谢链接)。

public class Node
{
    public Node(Node parent = null, params Node[] children) { }
    public Node(params Node[] children) { }
    public Node CreateNode() => new Node(new Node(), new Node()); }
}

What is the best way to deal with this whilst retaining the cross purpose constructor functionality (it allows for more expressive composition of code eg XElement).在保留跨目的构造函数功能的同时处理这个问题的最佳方法是什么(它允许代码的更具表现力的组合,例如 XElement)。

I was thinking maybe temporarily wrap or cast the parent , but that obviously introduces more boilerplate.我在想可能暂时包装或强制转换parent ,但这显然会引入更多样板。 Not sure if I could somehow leverage the ValueTuple<> syntax to get what i want?不确定我是否可以以某种方式利用ValueTuple<>语法来获得我想要的东西? Or implicit and explicit operators?!还是隐式和显式运算符?!

EDIT编辑

I wanted to create the nodes in a similar fashion to XElement, etc. eg我想以与 XElement 等类似的方式创建节点,例如

new Node(new Node(new Node())

however there were cases where I had already created a parent and wanted to pass that into the constructor of its child nodes eg但是在某些情况下,我已经创建了一个父节点并希望将其传递给其子节点的构造函数,例如

Node parent = new Node();
new Node(parent, new Node(), new Node());

Obviously the compiler has to chose one, and so I guess I was really asking how to force it to choose the other, so it knows the first argument should be treated as the parent.显然编译器必须选择一个,所以我想我真的在问如何强制它选择另一个,所以它知道第一个参数应该被视为父级。

Right now you are passing the parameter array in its "expanded form", which causes this rule to apply, making both methods an applicable function member .现在,您正在以“扩展形式”传递参数数组,这会导致应用此规则,从而使这两种方法都成为适用的 function 成员

If a function member that includes a parameter array is not applicable in its normal form, the function member may instead be applicable in its expanded form:如果包含参数数组的 function 成员不适用于其正常形式,则 function 成员可能适用于其扩展形式:

  • The expanded form is constructed by replacing the parameter array in the function member declaration with zero or more value parameters of the element type of the parameter array such that the number of arguments in the argument list A matches the total number of parameters.通过将function成员声明中的参数数组替换为参数数组的元素类型的零个或多个值参数来构造扩展形式,使得参数列表A中的arguments的数量与参数的总数匹配。 [...] [...]

And then the rules in better function member decide that the first one is a better function member.然后更好的function 成员中的规则决定第一个是更好的 function 成员。 Specifically, this one:具体来说,这个:

Otherwise, if Mp has more declared parameters than Mq , then Mp is better than Mq .否则,如果Mp具有比Mq更多的声明参数,则Mp优于Mq This can occur if both methods have params arrays and are applicable only in their expanded forms.如果这两种方法都有参数 arrays 并且仅适用于它们的扩展 forms,则可能会发生这种情况。

So to solve this problem, just pass your parameter in its "normal form", ie as an array.所以要解决这个问题,只需以“正常形式”传递您的参数,即作为数组。 It's not that much more keystrokes if you use an implicitly typed array.如果您使用隐式类型的数组,则击键次数并不多。

new Node(new[] { new Node(), new Node() })

This way, only one overload is an applicable function member.这样,只有一个过载是适用的 function 成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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