C# getter vs readonly

前端 未结 9 2187
遥遥无期
遥遥无期 2020-12-29 19:23

Is there any difference between the following?

class C
{
    // One:
    public static readonly int ValueAsAMember = 42;

    // Two:
    public static int V         


        
9条回答
  •  借酒劲吻你
    2020-12-29 20:09

    Yes, there is an advantage:

    If the value gets changeable at any point in the future (e.g. in a future version of your code), in a way that it is, for example, time-dependent, you can support that in the read-only property without changing the public interface of your class.

    If you have to replace a readonly field with a property, you will have to recompile any other assemblies that use your class.

提交回复
热议问题