C# 对象初始值设定项的复杂性。最佳实践

发布于 2024-09-06 07:00:05 字数 418 浏览 4 评论 0原文

当 C# 中出现对象初始值设定项时,我太兴奋了。

MyClass a = new MyClass();
a.Field1 = Value1;
a.Field2 = Value2;

可以重写得更短:

MyClass a = new MyClass { Field1 = Value1, Field2 = Value2 }

对象初始值设定项代码更明显,但是当属性数量达到十几个并且某些赋值处理可为空值时,很难调试“空引用错误”的位置。 Studio 将整个对象初始值设定项显示为错误点。

现在,我使用对象初始值设定项来进行直接赋值,仅用于无错误的属性。

如何使用对象初始值设定项进行复杂的分配,或者使用数十个分配是一种不好的做法?

先感谢您!

I was too excited when object initializer appeared in C#.

MyClass a = new MyClass();
a.Field1 = Value1;
a.Field2 = Value2;

can be rewritten shorter:

MyClass a = new MyClass { Field1 = Value1, Field2 = Value2 }

Object initializer code is more obvious but when properties number come to dozen and some of the assignment deals with nullable values it's hard to debug where the "null reference error" is. Studio shows the whole object initializer as error point.

Nowadays I use object initializer for straightforward assignment only for error-free properties.

How do you use object initializer for complex assignment or it's a bad practice to use dozen of assigments at all?

Thank you in advance!

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

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

发布评论

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

评论(3

你丑哭了我 2024-09-13 07:00:06

好吧,我对对象初始值设定项的主要不满是它们需要可变类型来开始:在可能的情况下,我更喜欢使用不可变类型。话虽如此,当对象初始化器工作时(例如对于UI控件)我倾向于使用它们。

如果赋值的值计算起来特别复杂,我可能会三思而后行 - 特别是,您必须能够在单个表达式中获取值,而这最终可能会比通过几个语句来计算它......但在开始可行的情况下这种情况相对较少。

我不能说在使用对象初始值设定项进行属性分配期间遇到任何异常问题 - 这不是我遇到的问题。如果确实如此,我可能会尝试编写一个失败的单元测试,此时代码通常无需调试器就可以轻松修复。

显然,适度总是一件好事 - 我并不建议采取极端......但如果您要设置十几个属性,那么使用对象初始值设定项对我来说不会像拥有十几个属性那样令人担忧设置开始。这十几个属性中有任何相关吗?应该以某种方式将它们封装在一起吗? (在某些情况下,这很好 - 再次强调,特别是对于 UI 控件 - 但通常情况并非如此。)

Well, my main beef with object initializers is that they require a mutable type to start with: where possible, I prefer to use immutable types. Having said that, when an object initializer will work (e.g. for UI controls) I tend to use them.

I would possibly think twice if the values for the assignments were particularly complicated to compute - in particular, you have to be able to get the value in a single expression, and that may end up being less readable than computing it over several statements... but that's relatively rare in situations where this is feasible to start with.

I can't say I've had any problems with exceptions during property assignments with object initializers - it's just not something that comes up for me. If it did, I'd probably try to write a failing unit test anyway, at which point code usually becomes easy to fix without the debugger anyway.

Obviously moderation is always a good thing - I'm not suggesting taking this to extremes... but if you're setting a dozen properties, using an object initializer wouldn't be as much of a concern to me as having a dozen properties to set to start with. Are any of these dozen properties related? Should those be encapsulated together somehow? (In some cases that is fine - again, particularly with UI controls - but often it's not.)

凉宸 2024-09-13 07:00:06

如果您正在调试应用程序,那么在大多数情况下您应该仍然能够使用 VS 的调试工具来确定哪个赋值导致了空引用。

但是,如果您将分配分配到多行,我认为 VS 将在引发异常时指向正确的行:

MyClass a = new MyClass {
                    Field1 = Value1,
                    Field2 = Value2 };

注意:我自己不会这样做,所以请对我(可能)可怕的换行风格保持温和。

If you're debugging your application then you should in most cases still be able to use VS' debugging tools to determine which assignment is causing the null reference.

However, if you split the assignment across multiple lines, I think VS will point to the right line when an exception is thrown:

MyClass a = new MyClass {
                    Field1 = Value1,
                    Field2 = Value2 };

Note: I don't do this myself, so be gentle about my (possibly) horrid line-breaking style.

娜些时光,永不杰束 2024-09-13 07:00:06

我从不在正在开发的代码中使用它,因为正如您所指出的,它更难以调试。

对于经过良好测试的代码,它可以使代码更具可读性,尽管可能有人会说,您不应该仅仅为了使其更易于阅读而搞乱经过良好测试和工作的代码。

请记住,对象初始化程序引入该语言主要是为了使 Linq 语法合法。这并不意味着它应该得到更广泛的使用。

话虽如此,如果您要在多个控件上设置一堆属性,则仅使用缩进就可以更轻松地快速了解某些控件的属性设置位置。

I never use it in code that is in development because as you point out, it is more difficult to debug.

For code that is well tested it can make the code more readable, although it could be argued that you shouldn't be messing with well tested and working code just to make it easier to read.

Remember, object initialisers where introduced to the language primarily to make the Linq syntax legal. That doesn't mean that it should be used more widely.

Having said that, if you are setting a bunch of properties on a number of controls, the indentation alone can make it a lot easier to get a quick overview of where certain controls are having their properties set.

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