Whats the utility of public constructors in abstract classes in C#?

前端 未结 3 1288
不思量自难忘°
不思量自难忘° 2020-12-15 17:29

If a public constructor in an abstract class can only be called by their derived classes it should be functionally equivalent to a protected constructor. Right?

Is t

3条回答
  •  有刺的猬
    2020-12-15 17:37

    You are correct. A public constructor in an abstract class is functionally equivalent to a protected constructor.

    I prefer to use a protected constructor in this case.

    While, it is true that the compiler will not complain about you doing this, the compiler will complain about trying to generate an instance of the abstract class. Visual Studio is smart enough, as well, to not provide Intellisense if you try to instantiate the abstract class.

提交回复
热议问题