Object initializer performance

后端 未结 7 1930
遇见更好的自我
遇见更好的自我 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条回答
  •  北海茫月
    2021-01-04 02:21

    I've not benchmarked it, but I'd be supprised if they didn't compile to the same thing.

    That said, It's easier for me to type when I need something like

    var bob = service.GetSingle( new Constraint { UserName = "Bob" } );
    

    I strongly dislike the repeated assignments of properties like

    var c = new Constraint();
    c.UserName = "Bob";
    c.Position = Positions.Manager;
    var bob = service.GetSingle( c );
    

提交回复
热议问题