Protected readonly field vs protected property

后端 未结 7 1762
广开言路
广开言路 2021-01-01 17:07

I have an abstract class and I\'d like to initialize a readonly field in its protected constructor. I\'d like this readonly field to be available in derived classes.

7条回答
  •  佛祖请我去吃肉
    2021-01-01 17:20

    Readonly properties were implemented in C# 6.0. This is as simple as:

    protected Foo(int field)
    {
        Field = field;
    }
    
    protected int Field { get; }
    

提交回复
热议问题