"new" to hide base class implementation - is it useless?
-
Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken!
class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }
dev
-
Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken!
class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }
dev
Why would you expect A.Yell to be called when you have a reference to B? ...or did you mean to place your "Always B.Yell..." comment at ref2? Kind Regards, Keld Ølykke
-
Why would you expect A.Yell to be called when you have a reference to B? ...or did you mean to place your "Always B.Yell..." comment at ref2? Kind Regards, Keld Ølykke
-
Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken!
class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }
dev
I am pretty sure that the sole purpose of the
new
keyword in this context, is to hide the warning and has no other effect. And to back it up :) :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 thenew
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.2A
-
Keld Ølykke wrote:
Why would you expect A.Yell to be called when you have a reference to B?
Then what good is decorating the method B.Yell with "new" keyword? In the example, it does absolutely nothing. (i.e. remove it, you still have same result)
dev
If you mean to call a base class method by inheritance there is 2 options: 1) inherited class does not override or new a base class method => B.Yell() will call A.Yell() in all cases 2) inherited class does override or new a base class method => B.Yell() has the full responsibility for calling base.Yell() You're example is not supposed to call base method, since you are missing a base.Yell() call in B - and it has nothing to do with the new keyword. Kind Regards, Keld Ølykke
-
Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken!
class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }
dev
The main intent of the
new
keyword in C# in this context is to let programmers and reviews know that this method is not an override and 'shadows' the base class method. This is specially targeted towards C++ programmers who migrated to C#. Remember, C++ does not have theoverride
keyword. Thewnew
keyword also enables you to 'shadow' or 'redefine' a virtual method in a derived class. IMO, this was not possible in C++. Correct me if I'm wrong. -
Take a simple example from here[^] The following example, try remove "new" from B.Yell, it makes no difference (except Compiler will bitch about it). Let me know if I am mistaken!
class Program { static void Main(string[] args) { try { A ref1 = new A(); A ref2 = new B(); B ref3 = new B(); ref1.Y(); <-- A.Yell ref2.Y(); <-- B.Yell ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless) } catch (Exception Ex) { Console.WriteLine("Main.Exception - " + Ex.ToString()); } return; } class A { public void Yell() { Console.WriteLine("A.Yell"); } } class B : A { public new void Yell() { Console.WriteLine("B.Yell"); } } }
dev
The
new
keyword does not make a difference in the case you are looking at. A bigger difference is made in the lineA ref2 = new B();
If you remove new and use the override keyword, your output will beA.Yell B.Yell
This article[^] explains the difference quite clearly.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
I am pretty sure that the sole purpose of the
new
keyword in this context, is to hide the warning and has no other effect. And to back it up :) :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 thenew
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.2A
-
If you mean to call a base class method by inheritance there is 2 options: 1) inherited class does not override or new a base class method => B.Yell() will call A.Yell() in all cases 2) inherited class does override or new a base class method => B.Yell() has the full responsibility for calling base.Yell() You're example is not supposed to call base method, since you are missing a base.Yell() call in B - and it has nothing to do with the new keyword. Kind Regards, Keld Ølykke
-
The
new
keyword does not make a difference in the case you are looking at. A bigger difference is made in the lineA ref2 = new B();
If you remove new and use the override keyword, your output will beA.Yell B.Yell
This article[^] explains the difference quite clearly.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
Abhinav S wrote:
A bigger difference is made in the line
A ref2 = new B();
You are wrong, I suggest you try it yourself. As pointed out by Markovl and myself this post, removing "new" makes no difference besides silencing compiler warning. Also, check out MSDN:[^] 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. This is just another useless facility only relevant in enterprise interviews.
dev
-
i know how to call the base class method - i am asking if "new" is useless, that its own relevance is to silence compiler warning. Read this.[^]
dev
Okay. I was in doubt about what you wanted to happen in your example. I have a problem with your word useless. I think you refer to compiled code not changing behavior or something similar. If so, I disagree with that opinion. In C# great efforts has been made to avoid that the programmer by accident does something he didn't mean to. One way that this is done, is by the introduction of more keywords e.g. virtual+override or new. This attitude from the language design team makes C# my favorite programming language. In short, no, I don't find it useless, and, yes, it seems to "just" hide a compiler warning :) Kind Regards, Keld Ølykke
-
Abhinav S wrote:
A bigger difference is made in the line
A ref2 = new B();
You are wrong, I suggest you try it yourself. As pointed out by Markovl and myself this post, removing "new" makes no difference besides silencing compiler warning. Also, check out MSDN:[^] 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. This is just another useless facility only relevant in enterprise interviews.
dev
I guess you are missing the whole point behind new. I mentioned this link[^] in my previous answer - am mentioning it again here - http://msdn.microsoft.com/en-us/library/ms173153%28v=vs.80%29.aspx[^]. The example clearly explains what
new
can do and whatoverride
does.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Okay. I was in doubt about what you wanted to happen in your example. I have a problem with your word useless. I think you refer to compiled code not changing behavior or something similar. If so, I disagree with that opinion. In C# great efforts has been made to avoid that the programmer by accident does something he didn't mean to. One way that this is done, is by the introduction of more keywords e.g. virtual+override or new. This attitude from the language design team makes C# my favorite programming language. In short, no, I don't find it useless, and, yes, it seems to "just" hide a compiler warning :) Kind Regards, Keld Ølykke
-
I guess you are missing the whole point behind new. I mentioned this link[^] in my previous answer - am mentioning it again here - http://msdn.microsoft.com/en-us/library/ms173153%28v=vs.80%29.aspx[^]. The example clearly explains what
new
can do and whatoverride
does.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial