Object initializer performance

后端 未结 7 2099
遇见更好的自我
遇见更好的自我 2021-01-04 01:47

Is the object initializer in c# 3.0 faster then the regular way?

Is this faster

Object object = new Object
{
    id = 1;
}

than th

7条回答
  •  萌比男神i
    2021-01-04 02:27

    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.

提交回复
热议问题