Overriding constants in derived classes in C#

后端 未结 6 1386
说谎
说谎 2020-12-09 14:50

In C# can a constant be overridden in a derived class? I have a group of classes that are all the same bar some constant values, so I\'d like to create a base class that def

6条回答
  •  心在旅途
    2020-12-09 15:26

    Unfortunately constants cannot be overridden as they are not virtual members. Constant identifiers in your code are replaced with their literal values by the compiler at compile time.

    I would suggest you try to use an abstract or virtual property for what you would like to do. Those are virtual and as such can (must, in the case of an abstract property) be overridden in the derived type.

提交回复
热议问题