1

I have seen methods that are declared like this:

public void new SortItems()

What does this actually do? I know that the new keyword is used to invoke constructors, but I have also seen it on method definitions like the example above.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331

4 Answers4

4

When use this way, it's a modifier. It's used to hide an inherited member rather than to override it. This is useful if the base method is sealed. Here's a quick example to demonstrate the difference between overriding and hiding inherited members:

public class Foo
{
    public virtual void OverriddenMethod() { Console.WriteLine("foo"); }
    public void HiddenMethod() { Console.WriteLine("foo"); }
}

public class Bar : Foo
{
    public override void OverriddenMethod() { Console.WriteLine("bar"); }
    public new void HiddenMethod() { Console.WriteLine("bar"); }
}

void Main()
{
    Bar bar = new Bar();
    Foo foo = bar;
    bar.OverriddenMethod(); // "bar"
    bar.HiddenMethod();     // "bar"
    foo.OverriddenMethod(); // "bar"
    foo.HiddenMethod();     // "foo"
}

Further Reading

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Just a short note: `new` modifier doesn't do anything with inheritance. In case of shadowing, it just suppress the compiler warning. – Alireza Nov 16 '13 at 19:05
3

It should be like this:

public new void SortItems(){
  //...
}

This new keyword is used to shadow the base member (method, property, ...) which has the same name (for property, event...) and same signature (for method), in this case it is the method SortItems. It's different from the new in creating new instance. No matter using new to shadow the conflicted member in base class or not, to access the base member you have to use the keyword base to access it in the derived class.

King King
  • 61,710
  • 16
  • 105
  • 130
1

When used in a method signature, it means that the implementation details are different for the class defining them. The problem with this approach is that it is not used polymorphically so:

class Thing
{
    void DoSomething()
    {
        Console.WriteLine("Thing");
    }
}

class Other : Thing
{
    new void DoSomething()
    {
        Console.WriteLine("Other");
    }
}

var thing = new Thing();
thing.DoSomething(); \\ prints Thing

var other = new Other();
other.DoSomething(); \\ prints Other

((Thing)other).DoSomething(); \\ prints Thing
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
  • I'd like to add that this should be avoided. All kinds of unexpected behavior can occur if this is excessively used. – PMF Nov 16 '13 at 18:54
0

It is the opposite of override. Say you have:

public class A
{
    public virtual void f() { Console.WriteLine( "A" ); }
}
public class B : A
{
    public override void f() { Console.WriteLine( "B" ); }
}

public class C : A
{
    public new void f() { Console.WriteLine( "C" ); }
}

And then in main:

A b = new B();
A c = new C();

b.f();
c.f();
(c as C).f();

This would print:

B
A
C

It will only call the new method when the type is that of the defining class.

clcto
  • 9,530
  • 20
  • 42