|
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!
<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
try<br />
{<br />
A ref1 = new A(); <br />
A ref2 = new B();<br />
B ref3 = new B();<br />
<br />
ref1.Y(); <-- A.Yell<br />
ref2.Y(); <-- B.Yell<br />
ref3.Y(); <-- Always B.Yell, regardless whether you decorated Yell() with "new" or not! (This makes the keyword useless)<br />
}<br />
catch (Exception Ex)<br />
{<br />
Console.WriteLine("Main.Exception - " + Ex.ToString());<br />
}<br />
<br />
return;<br />
}<br />
<br />
class A<br />
{<br />
public void Yell()<br />
{<br />
Console.WriteLine("A.Yell");<br />
}<br />
}<br />
<br />
class B : A<br />
{<br />
public new void Yell()<br />
{<br />
Console.WriteLine("B.Yell");<br />
}<br />
}<br />
}<br />
dev
modified 31-Jan-13 0:26am.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Thank you Keld, I was just looking for some sort of confirmation.
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 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.
2A
modified 31-Jan-13 3:31am.
|
|
|
|
|
Great! Thanks Markovl!
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 the override keyword.
Thew new 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.
modified 31-Jan-13 7:18am.
|
|
|
|
|
The new keyword does not make a difference in the case you are looking at.
A bigger difference is made in the line A ref2 = new B();
If you remove new and use the override keyword, your output will be
A.Yell<br />
B.Yell
This article[^] explains the difference quite clearly.
|
|
|
|
|
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 didn't miss anything Abhinav,
(a) I wasn't asking about 'override' or polymorphism
(b) *new* in context of method hiding really does nothing except to silence compiler warning
dev
|
|
|
|