Adding a “new” before the returned type of a C # method definition

So, I recently came across this C # statement:

public new string SomeFunction(int i)
{
     return base.SomeFunction(i);
}

I searched on the Internet, but I think I can find a better answer here.

Now I assume that all this returns a new line with the same value as the line returned by the call base.SomeFunction(i)... is this correct?

Also, does this function exist in other languages ​​(specifically for java)?

EDIT:
In my particular case, it base.SomeFunctionis protected and NOT virtual ... does it matter? Thanks

+3
source share
3 answers

, , SomeFunction , . , ( -, !)

. . ( , , , "" , , .)

, , ...

:

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

:

Derived.OverrideMe
Base.HideMe
+9

'new' - , . :

. , . , . , , , .

+1

, , . , , . , , , , , .

, , . , , .

+1

Source: https://habr.com/ru/post/1713355/


All Articles