What is the difference between Const and Static in C#?

前端 未结 5 1332
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 10:19

I am eager to know the difference between a const variable and a static variable.

As far as I know a const is also static and can not be accessed on instance variabl

5条回答
  •  猫巷女王i
    2020-12-08 10:46

    As you say, both static and const are attached to a type rather than an instance of a type. However, you can still change static items. You cannot change const items.

    Be careful with this, though. If your const item is a reference type, the assigned expression has to be evaluated at compile time, and that means that the only possible value you can give the reference is null (with the notable and useful exception of strings).

提交回复
热议问题