C# getter vs readonly

前端 未结 9 2183
遥遥无期
遥遥无期 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条回答
  •  梦毁少年i
    2020-12-29 19:58

    readonly is nice to use on things that can only be changed in your constructor. Examples of this is typical services as interfaces when you are following the TDD pattern.

    In your example const is best, it's a constant after all.

    readonly

    const

    Cheers

提交回复
热议问题