Which is better between a readonly modifier and a private setter?

前端 未结 5 868
遥遥无期
遥遥无期 2020-12-02 09:47

I\'ve been working on creating a class and suddenly a thought came to my mind of what is the difference between the two codes:

public readonly string Product         


        
5条回答
  •  清歌不尽
    2020-12-02 10:11

    The first is a field whose value can be set only at instantiation.

    The second is a property whose value can be set at any time (but only by its containing object).


    Correction: The property can be set at any time by any instance of the same class (and not only by its containing object).

提交回复
热议问题