3

Today I got to know that we can apply new inheritance modifier to const field also. I have used it for normal fields and methods and they make complete sense to me as they participate in inheritance hierarchy. I tried my level best to understand the applicability of this keyword to const fields also but couldn't. Here are few things that are coming to my mind:

Inheritance and run time polymorphism make sense in normal (non static) classes. Const fields are static by default. Also, static classes do not support inheritance in C#.

Let's say I've two classes as shown below:

class MyClass1
{
    public const string PI = "3.14";
}

class MyClass2 : MyClass1
{
    public const string PI = "3.141";   
}

For outside world they always access the constants present in these classes using ClassName.FieldName syntax e.g. MyClass1.PI or MyClass2.PI. So for outside world it doesn't matter if MyClass1 and MyClass2 are related through an inheritance hierarchy. MyClass1.PI fetches 3.14 and MyClass2.PI fetches 3.141.

If I'm inside an instance method of MyClass2 as shown below the class is fully aware if he is using the static member of his own class or static member of his base class. It uses MyClass1.PI syntax if he is referring to base class's static field. There is nothing which gets resolved at run-time.

class MyClass2 : MyClass1
{
    public const string PI = "3.141";
    string GetValueOfPi()
    {
        return PI;
    }

    string GetValueOfPiFromBaseClass()
    {
        return MyClass2.PI; //It clearly uses the syntax for accessing a static field of its base type
    }
}

Thus I'm not able to figure out the usage/relevance/significance of new inheritance modifier for const fields. In code snippet # 1 parser clearly gives me following warning:

'MyClass2.PI' hides inherited member 'MyClass1.PI'. Use the new keyword if hiding was intended.

But what is the piece of consumer code will get confused or break or fall into a problem if I don't use the new inheritance access modifier here (i.e. if I don't fix the warning). Can someone help me in understanding the concept involved in it?

RBT
  • 24,161
  • 21
  • 159
  • 240
  • Maybe because `const` variables can be accessed by both the class and the instance name? – Ian H. Dec 11 '16 at 10:00
  • `const` is form of static field. so same rules applies to const fields. just use new keyword if you want to get rid of warning. – M.kazem Akhgary Dec 11 '16 at 10:07
  • 1
    Possible duplicate of [What is the point of "static new" modifier for a function?](http://stackoverflow.com/questions/661246/what-is-the-point-of-static-new-modifier-for-a-function) – M.kazem Akhgary Dec 11 '16 at 10:09

2 Answers2

1

So your derived class has a const which you want to have different implementation. For const this could happen only if you use new keyword. For methods/properties you can define it as virtual in the base and override it in the derived class or again with new. Both new and override works in different way, but this is not the part of your question: Knowing When to Use Override and New Keywords if you want to understand when what to use it.

About your warning: If you don't specify new, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method and merely forgot to include the keyword).

mybirthname
  • 17,949
  • 3
  • 31
  • 55
1

But what is the piece of consumer code will get confused or break or fall into a problem if I don't use the new inheritance access modifier here (i.e. if I don't fix the warning).

The warning is telling you that if any code refers to MyClass1.PI, it may wrongly expect to use MyClass2.PI. Suppose we move GetValueOfPi to the base class:

class MyClass1
{
    public const string PI = "3.14";
    public string GetValueOfPi() { return PI; }
}
class MyClass2 : MyClass1
{
    public const string PI = "3.141";
}

Now, new MyClass2().GetValueOfPi() will return "3.14", not "3.141". It's not clear from the code whether the person who wrote it intended this. By adding the new keyword, you're simply telling the compiler "yes, this is really what I want".