What does the keyword “new” do to a struct in C#?

前端 未结 6 1828
傲寒
傲寒 2020-11-28 04:41

In C#, Structs are managed in terms of values, and objects are in reference. From my understanding, when creating an instance of a class, the keyword new causes

6条回答
  •  一个人的身影
    2020-11-28 05:05

    Using "new MyStuct()" ensures that all fields are set to some value. In the case above, nothing is different. If instead of setting ms.name you where trying to read it, you would get a "Use of possible unassigned field 'name'" error in VS.

提交回复
热议问题