tags:

views:

169

answers:

3

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

+9  A: 

No, it means that it's hiding SomeFunction in 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:

using System;

class Base
{
    public virtual void OverrideMe()
    {
        Console.WriteLine("Base.OverrideMe");
    }

    public virtual void HideMe()
    {
        Console.WriteLine("Base.HideMe");
    }
}

class Derived : Base
{
    public override void OverrideMe()
    {
        Console.WriteLine("Derived.OverrideMe");
    }

    public new void HideMe()
    {
        Console.WriteLine("Derived.HideMe");
    }
}

class Test
{
    static void Main()
    {
        Base x = new Derived();
        x.OverrideMe();
        x.HideMe();
    }
}

The output is:

Derived.OverrideMe
Base.HideMe
Jon Skeet
How would derived.HideMe ever be hit then?
Polaris878
By declaring the variable to be of type Derived instead of Base.
Jon Skeet
haha I missed that in your code. Makes sense, thanks much
Polaris878
+1  A: 

'new' is the member-hiding keyword. From the docs:

When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

Ben M
+1  A: 

The intent behind your sample code is to make the function public in the child, even though it was protected in the base. The language doesn't let you make a class member more visible in the child, so this instead declares a new function that happens to have the same name. This hides the base function, but then again, the caller wouldn't have had access to that one in the first place, while this function calls the one in the base.

In short, the code is a bit of a hack, but it does make sense. It's probably a hint that the base might need its functionality refactored, though.

Steven Sudit