Protected readonly field vs protected property

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

    abstract class Foo
    {
        protected readonly int _field;
    
        protected Foo(int field)
        {
            _field = field;
        }
    }
    

    Will be appropriate as you want the derived class to be aware of it. Classes not of type Foo won't have access to _field.

提交回复
热议问题