What do braces after C# new statement do?

前端 未结 7 1804
孤城傲影
孤城傲影 2020-12-13 08:29

Given the code below, what is the difference between the way position0 is initialized and the way position1 is initialized? Are they equivalent?

7条回答
  •  臣服心动
    2020-12-13 08:47

    They are equivalent, apart from one being easier to read than the other one.

    Also consider the case when you want to pass the new object along to somewhere else:

    var aList = new List();
    aList.Add( new Position() { x=3, y=4 } );
    

提交回复
热议问题