Why do I NOT get warnings about uninitialized readonly fields?

后端 未结 4 1825
醉酒成梦
醉酒成梦 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:37

    This is MSDN Documentation: Compiler Warning (level 4) CS0649:

    Field 'field' is never assigned to, and will always have its default value 'value'

    The compiler detected an uninitialized private or internal field declaration that is never assigned a value.

    So, for non-internal and non-private fields you shouldn't expect to have a warning.

    But I think the main reason is that C# compiler believes that you should initialize all the things that are accessible just from your assembly. I guess C# compiler left it to the others to initialize non-private and non internal fields in their assembly.

    But I tested protected internal and I don't know why C# compiler doesn't warn about it.

提交回复
热议问题