Overriding constants in derived classes in C#

后端 未结 6 1364
说谎
说谎 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:04

    Constants marked with const cannot be overridden as they are substituted by the compiler at compile time.

    But regular static fields assigned to constant values can. I've had such a case just now:

    class Columns
    {
        public static int MaxFactCell = 7;
    }
    
    class Columns2 : Columns
    {
        static Columns2()
        {
            MaxFactCell = 13;
        }
    }
    

    If I just redefined the MaxFactCell field in the derived class instead, polymorphism wouldn't work: code using Columns2 as Columns would not see the overriding value.

    If you need to restrict write (but not read) access to the field, using readonly would prohibit redefining it in Columns2. Make it a property instead, that's slightly more code:

    class Columns
    {
        static Columns()
        {
            MaxFactCell = 7;
        }            
        public static int MaxFactCell { get; protected set; }
    }
    
    class Columns2 : Columns
    {
        static Columns2()
        {
            MaxFactCell = 13;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:07

    Edit: This can have unexpected behaviour, see Shai Petel's remark below.

    You can hide the inherited constant in a derived class by declaring the new constant new. I'm not sure this is a good practice, though.

    class A
    {
        protected const int MyConst = 1;
    }
    
    class B : A
    {
        new private const int MyConst = 2;
    }
    
    0 讨论(0)
  • 2020-12-09 15:09

    to Work off dten + Tracker1's answer but updated for c# 6

    public class MyClass {
        public virtual string MyConst =>"SOMETHING";
    }
    ...
    public class MyDerived : MyClass {
        public override string MyConst  =>"SOMETHING ELSE";
    }
    
    0 讨论(0)
  • 2020-12-09 15:13

    It's not a constant if you want to override it ;). Try a virtual read-only property (or protected setter).

    Read-only property:

    public class MyClass {
        public virtual string MyConst { get { return "SOMETHING"; } }
    }
    ...
    public class MyDerived : MyClass {
        public override string MyConst { get { return "SOMETHINGELSE"; } }
    }
    

    Protected setter:

    public class MyClass {
        public string MyConst { get; protected set; }
    
        public MyClass() {
            MyConst = "SOMETHING";
        }
    }
    
    public class MyDerived : MyClass {
        public MyDerived() {
            MyConst = "SOMETHING ELSE";
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:19

    You can force derived classes to have a value for a constant (well, a read-only property)

    • Make an interface containing a read-only property.
    • Put that interface on the base class.

    Example:

       public interface IHasConstant
       {
            string MyConst { get; }
       }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题