gpt4 book ai didi

c# - 几个构造函数的链接c#

转载 作者:行者123 更新时间:2023-11-30 22:26:27 25 4
gpt4 key购买 nike

我做了一些谷歌搜索,但找不到任何东西。所以我实现了一些与中间构造函数一起工作的东西。

但是,我想知道是否可以同时链接多个构造函数(base + this)。

这是我当前的(简化的)代码:

public BlaNode(Node Previous, Node Next) : base(Previous, Previous)
{
this.Blas = new HashSet<BlaDiBla>();
}
public BlaNode()
: this(null, null)
{

}
public BlaNode(Node Previous, Node Next, string Bla)
: this(Previous, Previous)
{

}

最佳答案

不可以,您一次只能在 C# 构造函数初始值设定项中调用一个构造函数。 documentation 也仅引用一个要调用的其他构造函数:

All instance constructors (...) include an invocation of another instance constructor (...). The constructor to implicitly invoke is determined by the constructor-initializer:

如果您不想让任何其他代码调用它,您可以将中间构造函数设为private

关于c# - 几个构造函数的链接c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com