gpt4 book ai didi

c# - 在其自己的构造函数中将 "this"作为参数传递是一种不好的做法吗?

转载 作者:可可西里 更新时间:2023-11-01 08:48:47 25 4
gpt4 key购买 nike

我有这个BdlTabItem它接收类型为 DockableUserControl 的参数并想知道使用 uc.TabItem = this 在两者之间创建循环引用是否是一种不好的做法和 new BdlDockableWindow(this)在构造函数完成之前。

我知道这种行为对于非托管 native 代码 (C++) 来说非常糟糕。因此,即使我没有收到任何警告或错误,我也会在这里询问我是否应该这样做。

public BdlTabItem(BdlTabControl parent, DockableUserControl uc, string title)
{
TabControlParent = parent;
UserControl = uc;
WindowParent = new BdlDockableWindow(this);

this.Content = UserControl;

UserControl.TabItem = this;
}

最佳答案

这是可以接受的,但会引发问题。为什么选项卡项实例化了一个新的 WindowParent 但父选项卡控件没有对它的引用?或者为什么窗口父级不是传入的用户控件的属性?似乎这种行为应该在其他地方。

关于c# - 在其自己的构造函数中将 "this"作为参数传递是一种不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830683/

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