So I recently ran into this C# statement at work:
public new string SomeFunction(int i)
{
return base.SomeFunction(i);
}
I searched the web but I think I can find a better answer here.
Now, I’m guessing that all this does is return a new string with the same value as the string returned by the call to base.SomeFunction(i)… is this correct?
Also, does this feature exist in other languages (java specifically)?
EDIT:
In my specific case, base.SomeFunction is protected and NOT virtual… does this make a difference? Thanks
No, it means that it’s hiding
SomeFunctionin the base class rather than overriding it. If there weren’t a method in the base class with the same signature, you’d get a compile-time error (because you’d be trying to hide something that wasn’t there!)See this question for more information. (I don’t think this is a duplicate question, as it’s about what “new” is for at all rather than just talking about the warning when it’s absent.)
Duplicate example from my answer on that question though, just to save the clickthrough…
Here’s an example of the difference between hiding a method and overriding it:
The output is: