When should use Readonly and Get only properties

前端 未结 5 447
深忆病人
深忆病人 2020-12-02 05:00

In a .NET application when should I use \"ReadOnly\" properties and when should I use just \"Get\". What is the difference between these two.

private readonl         


        
5条回答
  •  一生所求
    2020-12-02 05:23

    As of C# 6 you can declare and initialise a 'read-only auto-property' in one line:

    double FuelConsumption { get; } = 2;
    

    You can set the value from the constructor but not other methods.

提交回复
热议问题