是否通过对象初始化设置属性:有什么区别吗?
之间有任何(性能)差异吗
Person person = new Person()
{
Name = "Philippe",
Mail = "[email protected]",
};
这是一个简单的问题: this :和 this
Person person = new Person();
person.Name = "Philippe";
person.Mail = "[email protected]";
?您可以想象具有更多属性的更大对象。
Here's a simple question : Is there any (performance) difference between this :
Person person = new Person()
{
Name = "Philippe",
Mail = "[email protected]",
};
and this
Person person = new Person();
person.Name = "Philippe";
person.Mail = "[email protected]";
You can imagine bigger object with more properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它们几乎完全相同,除了第一种方法(使用 对象初始值设定项 ) 仅适用于 C# 3.0 及更高版本。任何性能差异都很小,不值得担心。
它们生成几乎相同的 IL 代码。第一个给出了这个:
第二个给出了这个:
正如您所看到的,生成了几乎相同的代码。请参阅下面我编译的确切 C# 代码。
性能测量显示了非常相似的结果,但使用对象初始值设定项语法的性能改进非常小:
我用于测试性能的代码:
They are almost exactly equivalent except that the first method (using an object initializer) only works in C# 3.0 and newer. Any performance difference is only minor and not worth worrying about.
They produce almost identical IL code. The first gives this:
The second gives this:
As you can see, nearly identical code is generated. See below for the exact C# code I compiled.
Performance measurements show very similar results with a very small performance improvement for using the object initializer syntax:
Code I used for testing the performance:
另外一件值得注意的事情是:
如果您无法在构造函数中处理异常,您将收到 TypeInitializationException。虽然这看起来可能并没有那么糟糕,但事实是它掩盖了问题的真正原因,并且使追踪变得更加困难。
另一方面,如果您使用对象初始化程序,则您将在构造函数外部单独调用每个属性,并且任何抛出的异常都将非常清晰且明显:它们不会被 TypeInitializationException 掩盖。
一般来说,在构造函数中抛出异常是一个坏主意。如果您想避免这种情况,请使用初始化程序。
One additional thing worth noting is this:
If you fail to handle an exception in your constructor, you'll get a TypeInitializationException. While that may not seem so bad, the truth is that it conceals the real cause of the problem, and makes it harder to track down.
If, on the other hand, you use an object initializer, you're invoking each property individually outside of the constructor, and any thrown exceptions will be very clear and very evident: they won't be masked by the TypeInitializationException.
In general, it's a bad idea to throw exceptions in a constructor. If you want to avoid that scenario, go with the initializer.
正如其他人所说,不,没有区别。请注意,第一个示例实际上并未对这些参数使用构造函数。它使用 C# 3.0 中引入的“对象初始值设定项”语言功能。就像第二个示例一样,被调用的构造函数是默认的无参数构造函数。
这两个示例实际上编译为(几乎)相同的 IL 代码,并且出于所有意图和目的执行相同的操作。第一个示例只是一个相对较新的语法,以更简单且更具表现力的方式完成任务“意见”。
As others have said, no, there is no difference. Note that the first example isn't actually using a constructor for those arguments. It's using the "object initializer" language feature introduced in C# 3.0. The constructor being called is the default parameterless constructor just like the second example.
The two examples actually compile down to (nearly) the same IL code and for all intents and purposes do the same thing. The first example is just a relatively newer syntax to accomplish the task <opinion>in an easier and more expressive way</opinion>.
不是。第一种方法是 .NET 3.5 中的新方法,但第二个示例适用于以前版本的 C#。
No. The first way is new in .NET 3.5 but the second example is for previous versions of C#.
正如其他答复所示,性能方面没有显着差异。
然而,在我看来,使用带有 2 个参数的初始化程序创建一个对象就像您向使用它的任何人陈述您的意图,形成一个“契约”:“这 2 个参数是功能的最小值类的”(尽管表达该意图的正确方法是使用构造函数)。
我倾向于以这种方式思考初始化语法,尽管它或多或少只是语法糖。我在代码中混合使用了两种语法。但话又说回来,这就是我的个人风格。
Performance-wise there is no significant difference, as other replies have shown.
However, creating an object using an initializer with 2 parameters seems to me like you state your intent to anyone who is using it, forming a "contract" saying: "these 2 parameters are the minimum for the functionality of the class" (although the correct way to express that intent would be to use a constructor).
I tend to think of the initializer syntax this way, although it's more or less just syntactic sugar. I use a mix of both syntaxes in my code. But then again, that's my personal style.