Mohsen Sarkar
Mohsen Sarkar

Reputation: 6030

How to override constants derived classes?

CODE :

public class A {
    public const int beingSupportedRate = 0;
}

 public partial class B : A {
    public const int beingSupportedRate = 1;

}

I want it as explicitly as a const int because of performance. Putting virtual in front of class A variable beingSupportedRate causes compiler error following :

The modifier 'virtual' is not valid for this item

Upvotes: 9

Views: 9681

Answers (5)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Actually I believe you misunderstood the point of polymorphism in object-oriented programming.

Constants, fields and variables are just a storage (well, references, but I'm talking from the conceptual point of view).

Polymorphism is about changing the behavior of something. Overriding a constant couldn't be changing a behavior but changing its value.

Another point is a constant is static, thus it doesn't belong to an instance but there's an immutable single value in the AppDomain and it survives for the application life-cycle.

With the above statement, why you would want to override a constant like an instance member? Do you imagine the next situation?

public class A 
{
      public virtual const int Some = 1;
}

public class B : A
{
      public override const int Some = 2;
}

public class C : A
{
     // No override here!
}

int valueOfSomeConstant = C.Some;

Hold! If a constant is static, C.Some would be 2 even if C does override no constant!

Some quote from your question:

I want it as explicitly as a const int because of performance. [...]

This has only an answer: the premature optimization is the devil of any software development.

As Jon Skeet said, this is going to be the least of your issues.

Upvotes: 7

John Willemse
John Willemse

Reputation: 6698

You should use the new keyword to explicitly hide the inherited member:

public class A
{
    public const int beingSupportedRate = 0;
}

public class B : A
{
    public new const int beingSupportedRate = 1;
}

Remember that you cannot access the constant member from an instance.

Console.WriteLine(A.beingSupportedRate);
Console.WriteLine(B.beingSupportedRate);

Output:

0
1

There are some problems that you should consider when using this solution. Take the following console program, for example:

class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();
        C c = new C();

        a.GetBeingSupportRate();
        b.GetBeingSupportRate();
        c.GetBeingSupportRate();

        Console.Read();
    }

    public class A
    {
        public const int beingSupportedRate = 0;
        public void GetBeingSupportRate()
        {
            Console.WriteLine(beingSupportedRate);
        }
    }

    public class B : A
    {
        public new const int beingSupportedRate = 1;

    }

    public class C : B
    {

    }
}

This will output 0 for all three class instances, since the inherited method uses the constant value in A. This means you will have to override all methods that reference the constant.

A preferred approach is to use an interface with a property that must be implemented and not use constants for this purpose.

Upvotes: 14

Ipad
Ipad

Reputation: 392

U Can do this i guess:

public class A 
{
    public virtual Int32 beingSupportedRate
    { 
        get { return 0; }
    }
}

public class B : A
{
    public override Int32 beingSupportedRate 
    {
        get { return 1; }
    }
}

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500873

Fields (including constants) can't be virtual. That has nothing to do with it being a constant... it's just the way fields work... although the fact that constants are implicitly static makes it even less feasible, as it were.

If you want polymorphic behaviour, it has to be via an instance member which is a property, method or event.

As an aside, I strongly suspect your "I want it as const for performance reasons" justification is bogus micro-optimization. It's not even clear how you're using this, but I very much doubt that you've tried it as a non-constant and proved that it's too slow.

Upvotes: 9

Grant Thomas
Grant Thomas

Reputation: 45083

Constants cannot be overridden, they are constant.

If you want this value to be alterable by extension then you'll need to use something less constant, with a nature of being changed per context, such as an abstract element to implement or virtual to override.

Upvotes: 5

Related Questions