Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

C#
public class ClassA
{
public   void MethodNew()
{
Console.WriteLine("In MethodNew of ClassA");
}

public virtual void MethodOverride()
{
Console.WriteLine("In MethodOverride of ClassA");
}
}

    public class ClassB : ClassA
    {
        public new void MethodNew()
        {
            Console.WriteLine("In MethodNew of ClassB");
        }

        public override void MethodOverride()
        {
            Console.WriteLine("In MethodOverride of ClassB");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ClassA a = new ClassB();
            a.MethodNew();
            a.MethodOverride();
        }
    }


In the above sample eventhough I append or not new keyword for Methodnew function in the derived class,the o/p is always calling method in base class...

So what is the purpose of using new here?
Posted
Updated 8-Feb-11 0:09am
v5

That's the correct behaviour, since:
  • You're using polymorphism to create ClassB instance.
  • Base class' MethodNew is NOT virtual.


new modifier just allow you to explicitely state you're hiding a base class member, see MSDN[^].
:)
 
Share this answer
 
Comments
siva455 8-Feb-11 6:15am    
Hi Cpallani,
Thanks for your answer...but im getting the same o/p even
without using new modifier..so as per my understanding new modifier
is used only to avoid compiler to show warning...correct me if im wrong...
#realJSOP 8-Feb-11 8:08am    
You are, in effect, casting the nelwly instantiated object to its based class, and therefore, you cannot use any of CLassB's methods. The fact that the warning is hidden merely cleans up the output window during the compile.
CPallini 8-Feb-11 6:18am    
Yes, the net effect in your case is warning message removal.
siva455 8-Feb-11 6:21am    
please let if there is anycase where new modifier(when used for methods) is useful other than removing warning message....
CPallini 8-Feb-11 6:26am    
There isn't, from MSDN: "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."
I thing the question is about modifier new as in public new void MethodNew(), not operator new used for initialization. The modifier new is absolutely different thing, it's used to dismiss compiler warning when hiding a member.

Consider the following:

C#
class Base {
    protected void Visible() { }
} //class Base

class AccedentallyHiding : Base {
    //will shows warning: Visible() hides inherited member
    //Base.Visible(). Use the new keyword if hiding is intended
    protected void Visible() { }
} //class AccedentallyHiding

class IntentionallyHiding : Base {
    //no problem: keyword "new" shows intended hiding
    protected new void Visible() { }
} //class IntentionallyHiding



Please see code comments explaining compiler behavior.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Feb-11 17:31pm    
Who was so clever to vote 1? This is probably the only correct answer about the modifier.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900