Why do I NOT get warnings about uninitialized readonly fields?

后端 未结 4 1818
醉酒成梦
醉酒成梦 2020-12-18 19:00

The C# compiler is kind enough to give you a \"field is never assigned to\" warning if you forget to initialize a readonly member which is private or internal, or if the cla

4条回答
  •  攒了一身酷
    2020-12-18 19:33

    I think it's because the warning related to the scope and usage. Public and protected member can be accessed outside the assembly scope and by the class consumer, and the developer might actually want the value of the variable to be its default.

    On the other hand, private and internal members are used internally, and you make it readonly because you do not want yourself to mistakenly change it, so it's highly likely that it should be initialised somewhere.

提交回复
热议问题