2

只是一个快速的外观问题......在你声明它们的地方初始化完整属性的私有字段是否可以接受,或者普通开发人员会诅咒我的名字没有把它放在构造函数中?

private int _linesDataGridHeight = 300;
public int LinesDataGridHeight
{
    get { return _linesDataGridHeight; }
    set { SetProperty(ref _linesDataGridHeight, value); }
}

private bool _isHideLinesCheckboxChecked = false;
public bool IsHideLinesCheckboxChecked
{
    get { return _isHideLinesCheckboxChecked; }
    set
    {
        this.LinesDataGridHeight = value ? 0 : 300;
        SetProperty(ref _isHideLinesCheckboxChecked, value);
    }
}
4

1 回答 1

2

我认为这主要取决于个人喜好,因为在 C# 中它并不重要。我更喜欢在构造函数中初始化所有内容,因为在那里我可以在一个地方看到我所有的初始化值。此外,您的开发团队可能为此制定了一个商定标准的规则。

这似乎是与另一个 SO 问题类似的帖子,请参见此处:Initialize class fields in constructor or at declaration?

于 2013-08-22T15:39:24.513 回答