Protected readonly field vs protected property

后端 未结 7 1700
广开言路
广开言路 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:35

    Using a private field and a protected getter has a slight advantage: you don't cause an additional level of indirection.

    Don't forget the c# shorthand way of declaring properties:

    protected int Field { protected get; private set; }
    
    0 讨论(0)
提交回复
热议问题