What do braces after C# new statement do?

前端 未结 7 1805
孤城傲影
孤城傲影 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:58

    Forgetting about all the IL stuff, it is just shorthand notation. What you are doing is this:

    a. In one case you are explicitly using the default constructor and then setting the two properties.

    b. In the other, you are using the new intializer syntax which implicitly makes the compiler do what you did in case a.

    IL subtelties notwithstanding, they will achieve the same thing for you.

提交回复
热议问题