Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding constants in derived classes in C#

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

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.


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

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

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