对象初始化器性能 [英] Object initializer performance
本文介绍了对象初始化器性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#3.0中对象初始化快于常规的方法是什么?
这是更快
Object对象=新对象
{
的id = 1;
}
比这个?
Object对象=新的对象()
object.id = 1;
解决方案
在发行模式下,它们将编译到的确切的相同IL code(假设你实际上是使用类型与 ID
属性,而不是对象
)
因此,将不根据定义没有性能差异。
我不知道哪一个会编得更快,但它会在编译时间分差,你可能不关心,
不过,对象初始化语法中快(少打字),所以你应该使用它。
Is the object initializer in c# 3.0 faster then the regular way?
Is this faster
Object object = new Object
{
id = 1;
}
than this ?
Object object = new Object()
object.id = 1;
解决方案
In Release mode, they will compile to the exact same IL code (assuming that you're actually using a type with an id
property instead of Object
)
Therefore, there will by definition be no performance difference.
I don't know which one will compile faster, but it would be a minute difference in compilation time and you probably don't care,
However, the object initializer syntax is written faster (less typing), so you should probably use it.
这篇关于对象初始化器性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文