6

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
Community
  • 1
  • 1
theateist
  • 13,879
  • 17
  • 69
  • 109
  • The new is used to define a new instance of the Class.. this is also true for objects in C# StringBuilder strb; would error if you did not "NEW" it up as we say so you create an instance of it example strb = new StringBuilder(1000) for example – MethodMan Feb 08 '12 at 21:04
  • also look at your example the dead give away from what I see is public override void One(); public new void Two(); look at basic OOP principles as well there are lots of Tutorials on the web as well – MethodMan Feb 08 '12 at 21:06
  • @DJKRAZE this use of `new` has nothing to do with creating objects: http://msdn.microsoft.com/en-us/library/51y09td4(v=vs.71).aspx#vclrfnew_newmodifier – phoog Feb 08 '12 at 21:38
  • theateist, I think the confusion about which `new` keyword you're asking about is partly due to the lack of declared method implementations. I'll edit your sample code. – phoog Feb 08 '12 at 21:50
  • 1
    phoog, you're right. I've also edited my post - **in method signature** is added – theateist Feb 08 '12 at 22:00
  • possible duplicate of [benefit of using new keyword in derived class member having same name with base class member](http://stackoverflow.com/questions/8229171/benefit-of-using-new-keyword-in-derived-class-member-having-same-name-with-base) – phoog Feb 09 '12 at 15:10

5 Answers5

9

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

Community
  • 1
  • 1
phoog
  • 42,068
  • 6
  • 79
  • 117
  • 7
    This is the correct answer, though I would add to it: that "new" is *optional* does indeed prevent the *brittle base class build break* that you describe. But it serves an additional purpose: it *informs you* that the provider of your base class has messed with it in a way that directly impacts you, the author of the subclass. The warning is waving a big red flag that says "hey, derived class implementor! Something important is going on here that you need to pay attention to!" We're not going to break your build, but we are going to let you know about it so that you can decide what to do. – Eric Lippert Feb 08 '12 at 22:35
  • @EricLippert thanks for pointing that out. See edited post for additional comments. – phoog Feb 09 '12 at 15:01
  • "brittle base class build break" i found a new tounge twister. – John Smith Sep 22 '15 at 02:50
  • so... the answer is `Yes` it's not required – Don Cheadle Mar 25 '16 at 20:45
  • 1
    @mmcrae the question is not "is it required"; the question is "is it only for readability" -- and the answer to that is `No` – phoog Mar 25 '16 at 22:41
  • the behavior (of method-hiding in this case) is the same with or without it. There's no functional change when having or not having `new`. Sounds like it really only accomplishes readability (`waving a big red flag` is an example of readability - it helps the developer read/understand what they're doing). For you to demonstrate that this is not the case, wouldn't you have to show that it has a functional/behavioral affect? – Don Cheadle Mar 28 '16 at 14:07
  • @mmcrae You do realize that someone on the C# language team (at the time) has said that this is the correct answer, don't you? It seems to me that this answer and his comment have established that one purpose of `new` is to address the brittle base class problem, regardless of the functional or behavioral effect of the keyword, or lack thereof. Why do you think otherwise? Do you think that `new` has no benefit in relation to the brittle base class problem? – phoog Apr 12 '16 at 23:35
  • @EricLippert I think would agree that there is no functionality different between having `new` used or not used in the subclass method's definition. It's just to help the developer **read** / understand that they're hiding a base method. I think you're mistaken, phoog, about the benefit that `new` brings to the brittle base class problem. The use of `new` to point out that you're overriding/hiding a base method has nothing to do with the brittle base class problem. The fact that `new` is optional, not required after the base class changes, is what prevents the brittle base class problem. – Don Cheadle Apr 14 '16 at 21:41
  • It's odd - your answer is about the fact that `new` being optional prevents the brittle base class problem. And it was **that** answer/idea that @EricLippert endorsed, as I see it. But from your comments it seems you're trying to state more - such that it's not purely just for readability. I agree with your answer - I upvoted it a while ago actually. But trying to say that `new` does anything more than readability is simply wrong. There's no semantic/functional difference. – Don Cheadle Apr 14 '16 at 21:49
  • @mmcrae Okay, I see your point. I just re-read the question, and I would rephrase it as "new is optional; why choose to use it? Only for readability?" And I still answer "no," without reference to the brittle base class problem, because the new keyword tells the reader that the programmer hid the base method knowingly. It doesn't necessarily make the code more readable, but it does let the author of the code say "I'm doing this on purpose." – phoog Apr 14 '16 at 22:01
  • `it *tells* the *reader* that the programmer hid the base method knowingly... doesn't make the code more readable, but it does let the *author* of the code *say* ...` ?? Here we are again. Sounds like an argument of readability. Hence I've finally added my own answer to this question. – Don Cheadle Apr 14 '16 at 22:20
  • @mmcrae perhaps we define readability differently. To me, something that's more readable is easier to read; altering the meaning of the message is a more fundamental change. Since the new keyword *conveys meaning,* it's not simply about making the meaning easier to read; it's about changing the meaning. Your argument seems equivalent to "the word *threw* is only for added readability in the sentence *the fielder threw the ball* because it *tells* the *reader* something that the *author* of the sentence wanted to *say.* By that argument, *everything* is only for added readability. – phoog Apr 14 '16 at 22:52
  • @mmcrae in other words `DataAccess dataAccess = DataAccess.CreateFromName("Some name")` is more readable than `DtaXS dtxs = DtaXS.Cre8FrmNm("Some name")`. The author's message to the reader is no different. But when you add `new` to a hiding method you change the content of the message. – phoog Apr 14 '16 at 22:56
5

My guess is that C# designers wanted us to be aware that by not using virtual and override we will not get a polymorphic behavior. And this is what most folks coming from Java would expect. The new makes this clear as omitting it causes the compiler to raise a warning.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
2

No, it's not for "readability"! It's there to make explicitly clear, that you don't want to override a inherited virtual message or "shadow" an inherited method by accident, but really want to give an new implementation of a method.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • 1
    It _is_ for readability. Without `new` the semantics are exactly the same. – H H Feb 08 '12 at 21:38
  • So again, it just makes clear when we're reading the code. It reminds me or who reads the code that there is a method, variable or property in the base class with the same name, right? – theateist Feb 08 '12 at 21:52
  • @theate - and it suppresses the warning. – H H Feb 08 '12 at 22:01
1

Its not required but I would strongly recommend it.

MSDN

A variable was declared with the same name as a variable in a base class. However, the new keyword was not used. This warning informs you that you should use new; the variable is declared as if new had been used in the declaration.

  public static int i = 2;   // CS0108, use the new keyword
  // the compiler parses the previous line as if you had specified:
  // public static new int i = 2;
MRB
  • 3,936
  • 1
  • 14
  • 6
  • @DJ Its the same compiler warning (CS0108) and concept. They don't give examples for all cases. – MRB Feb 08 '12 at 21:22
0

Yes, it is just there for readability.

The functionality is the same with or without it in the subclass's method signature.

... no one came out and said it yet because they were trying to point out why you still might want to use it. But any reason to use it is in support of readability.

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54