使用属性初始化时如何格式化多个属性? (。网)

发布于 2024-07-05 08:57:17 字数 275 浏览 8 评论 0原文

例如:

root.Nodes.Add(new TNode() { Foo1 = bar1, Foo2 = bar2, Foo3 = bar3 });

或:

root.Nodes.Add(new TNode() { Foo1 = bar1, 
                             Foo2 = bar2, 
                             Foo3 = bar3 });

For example:

root.Nodes.Add(new TNode() { Foo1 = bar1, Foo2 = bar2, Foo3 = bar3 });

or:

root.Nodes.Add(new TNode() { Foo1 = bar1, 
                             Foo2 = bar2, 
                             Foo3 = bar3 });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-07-12 08:57:18

两种记法都很好。 我只是建议,每当您的行保持在 100 个字符以内时,就使用第一个(单行)表示法,而每当表达式较长时,请切换到第二个(多行)表示法。

Both notations are fine. I would simply suggest to use the first (1-line) notation whenever your line stay within 100 characters, and switch to the second (multi-line) notation whenever the expression is longer.

寂寞花火° 2024-07-12 08:57:18

对于较长的东西我这样做:

root.Nodes.Add(new TNode() {
    Foo1 = bar1, 
    Foo2 = bar2, 
    Foo3 = bar3
});

For longer stuff I do it this way:

root.Nodes.Add(new TNode() {
    Foo1 = bar1, 
    Foo2 = bar2, 
    Foo3 = bar3
});
不顾 2024-07-12 08:57:17

我已经用两种方法完成了。在我看来,这取决于初始化的复杂性。

如果它是简单的 2 或 3 个属性,我通常会在一行上初始化,但如果我正在设置一个带有要插入数据库的值的对象或具有很多属性的对象,我会像第二个示例一样将其分解。

Income income = new Income
{
    Initials = something,
    CheckNumber = something,
    CheckDate = something,
    BranchNumber = something
};

或者

return new Report.ReportData { ReportName = something, Formulas = something};

I've done it both ways.. IMO it depends on the complexity of the initialization.

If it is simple 2 or 3 properties I will initialize on one line generally, but if i'm setting up an object with values for insertion into a database or something that has alot of properties i'll break it out like your second example.

Income income = new Income
{
    Initials = something,
    CheckNumber = something,
    CheckDate = something,
    BranchNumber = something
};

or

return new Report.ReportData { ReportName = something, Formulas = something};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文