How to have abstract and overriding constants in C#?

c#, constants, polymorphism

Solution

If your constant is describing your object, then it should be a property. A constant, by its name, should not change and was designed to be unaffected by polymorphism. The same apply for static variable.

You can create an abstract property (or virtual if you want a default value) in your base class:

public abstract string Bank { get; }

Then override with:

public override string Bank { get { return "Some bank"; } }

Problem

My code below won't compile. What am i doing wrong? I'm basically trying to have a public constant that is overridden in the base class. ``` public abstract class MyBaseClass { public abstract const string bank = "???"; } public class SomeBankClass : MyBaseClass { public override const string bank = "Some Bank"; } ``` Thanks as always for being so helpful!

Original source

Related problems