Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using new keyword in method signature generally just for readability?

Tags:

c#

c#-4.0

I've read about new keyword in method signature and have seen the example below on this post, but I still don't get why to write new keyword in method signature. If we'll omit it, it still will do the same things. It will compile. There is gonna be a warning, but it will compile.

So, writing new in method signature is just for readability?

public class A
{
   public virtual void One() { /* ... */ }
   public void Two() { /* ... */ }
}

public class B : A
{
   public override void One() { /* ... */ }
   public new void Two() { /* ... */ }
}

B b = new B();
A a = b as A;

a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B
like image 980
theateist Avatar asked Feb 08 '12 21:02

theateist


People also ask

Why we use new keyword in C# with method?

Use the new keyword to create an instance of the array. The new operator is used to create an object or instantiate an object.

When to use override and new keywords in c#?

The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class. The new keyword is used to hide a method, property, indexer, or event of base class into derived class.

What is the new keyword in C#?

The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.

Should you always use this in Java?

Using this to refer to attributes of a class within the class is redundant, and so increases code verbosity with no clear benefit. Not only can you almost always look up the status of a variable via the tools you use; more importantly, if you have to look it up, then your class is too big in the first place.


1 Answers

Implicit in this question: why isn't the new keyword required when hiding a base class member? The reason is the brittle base class problem. Suppose you have a library:

public class Base
{
    public void M() { }
}

and you've derived a class in your own code base:

public class Derived : Base
{
    public void N() { }
}

Now, the library authors release a new version, adding another method to Base:

public class Base
{
    public void M() { }
    public void N() { }
}

If the new keyword were required for method hiding, your code now fails to compile! Making the new keyword optional means that all you now have is a new warning to worry about.

EDIT

As Eric Lippert points out in his comment, "new warning to worry about" drastically understates the purpose of the warning, which is to "wave a big red flag." I must have been in a hurry when I wrote that; it's annoying when people reflexively view warnings as annoyances to be tolerated rather than treating them as useful information.

EDIT 2

I finally found my source for this answer, which, of course, is one of Eric's posts: https://stackoverflow.com/a/8231523/385844

like image 92
phoog Avatar answered Sep 19 '22 23:09

phoog