How to have abstract and overriding constants in C#?

23,110

Solution 1

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"; } }

Solution 2

What you are trying to do cannot be done. static and const cannot be overridden. Only instance properties and methods can be overridden.

You can turn that bank field in to a property and market it as abstract like the following:

public abstract string Bank { get; }

Then you will override it in your inherited class like you have been doing

public override string Bank { get { return "Charter One"; } }

Hope this helps you. On the flip side you can also do

public const string Bank = "???";

and then on the inherited class

public const string Bank = "Charter One";

Since static and const operate outside of polymorphism they don't need to be overriden.

Solution 3

In case you want to keep using "const", a slight modificaiton to the above:

public abstract string Bank { get; } 

Then override with:

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

And the property will then return your "const" from the derived type.

Share:
23,110
Chris
Author by

Chris

iOS developer / contractor based in Sydney.

Updated on July 09, 2022

Comments

  • Chris
    Chris almost 2 years

    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!

  • Chris
    Chris over 14 years
    Why can't it be done? Sounds like a design decision, and most design decisions in C# have valid & interesting reasons behind them IMO so i'm curious.
  • Joe Pitz
    Joe Pitz over 14 years
    the compiler does a replace with literal values at compile time
  • Jerph
    Jerph over 14 years
    static and const are better thought of as Global definitions, meaning that they don't abide by polymorphism. And can be called with out instantiating the class. Given that I have updated my post. You can do what you want, but you cannot really use polymorphism to accomplish it.
  • Jerph
    Jerph over 14 years
    @Joe Pitz: The compiler will not replace a property with a literal at compile time. Especially for overriden properties, because of the base type it could be any number of values that inherit from the base type.
  • Joe Pitz
    Joe Pitz over 14 years
    consts are replaced at compile time: Here is a quote from Mircosoft: :In fact, when the compiler encounters a constant identifier in C# source code (for example, months), it substitutes the literal value directly into the intermediate language (IL) code that it produces. " msdn.microsoft.com/en-us/library/ms173119.aspx
  • Sunkas
    Sunkas over 11 years
    I had to add the "new" keyword to the override code to not have a warning.
  • Pierre-Alain Vigeant
    Pierre-Alain Vigeant over 11 years
    The "new" keyword is needed when you are overriding a parent function which was not declare as "virtual" or "abstract"
  • Gobe
    Gobe over 8 years
    Unfortunately it does not return as const, if I try to access SomeBankClass.Bank I get "An object reference is required for the non-static field, method, or property 'SomeBankClass.Bank'", which completely makes sense. When Bank was a public const string in the SomeBankClass, this access was fine.
  • JuanP. Zuniga
    JuanP. Zuniga over 3 years
    "A constant, by its name, should not change": it should be added that the constant modifier also adds the static modifier, so it's different from just a read-only property. i.e. public string Bank { get => "something"; } will make Bank only be reachable in class instances, while public const string Bank = "something"; will make Bank usable without needing an instance of the class.
  • Paul Childs
    Paul Childs over 3 years
    "by its name... was designed to be unaffected by polymorphism" You can't really make such an argument. const as a computational term predates the concept of polymorphism.
  • Pierre-Alain Vigeant
    Pierre-Alain Vigeant over 3 years
    @PaulChilds feel free to propose a change to the answer. This is a collaborative website.