'Static readonly' vs. 'const'

前端 未结 18 3026
旧巷少年郎
旧巷少年郎 2020-11-22 04:07

I\'ve read around about const and static readonly fields. We have some classes which contain only constant values. They are used for various things

18条回答
  •  清歌不尽
    2020-11-22 04:58

    A few more relevant things to be noted:

    const int a

    • must be initialized.
    • initialization must be at compile time.

    readonly int a

    • can use a default value, without initializing.
    • initialization can be done at run time (Edit: within constructor only).

提交回复
热议问题