locked
virtual methods in c# RRS feed

  • Question

  • Do we have virtual methods in c#?

    If yes please provide resource or detail, thank you


    Regards Kumar Gaurav.
    • Moved by CoolDadTxModerator Friday, July 22, 2011 1:48 PM Language specific (From:Visual C# General)
    Friday, July 22, 2011 9:11 AM

Answers

  • Hello urprob

    A virtual method is a method that can be overridden in a derived class using the override, replacing the behavior in the superclass. If you don't override, you get the original behavior. If you do, you always get the new behavior. This opposed to not virtual methods, that can not be overridden but can hide the original method. This is done using the new modifier.

    _______________________________________________________________________________________________________

    public class BaseClass

    {

        public void SayHello()

        {

            Console.WriteLine("Hello");

        }

     

     

        public virtual void SayGoodbye()

        {

            Console.WriteLine("Goodbye");

        }

     

        public void HelloGoodbye()

        {

            this.SayHello();

            this.SayGoodbye();

        }

    }

    public class DerivedClass

    {

        public new void SayHello()

        {

            Console.WriteLine("Hi There");

        }

     

     

        public override void SayGoodbye()

        {

            Console.WriteLine("See you later");

        }

    }

     

     ____________________________________________________________________________

    When I instantiate DerivedClass and call SayHello, or SayGoodbye, I get "Hi There" and "See you later". If I call HelloGoodbye, I get "Hello" and "See you later". This is because SayGoodbye is virtual, and can be replaced by derived classes. SayHello is only hidden, so when I call that from my base class I get my original method.

    Thanks

    sankar


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Friday, July 22, 2011 9:31 AM
  • Friday, July 22, 2011 9:12 AM

All replies

  • Friday, July 22, 2011 9:12 AM
  • Hello urprob

    A virtual method is a method that can be overridden in a derived class using the override, replacing the behavior in the superclass. If you don't override, you get the original behavior. If you do, you always get the new behavior. This opposed to not virtual methods, that can not be overridden but can hide the original method. This is done using the new modifier.

    _______________________________________________________________________________________________________

    public class BaseClass

    {

        public void SayHello()

        {

            Console.WriteLine("Hello");

        }

     

     

        public virtual void SayGoodbye()

        {

            Console.WriteLine("Goodbye");

        }

     

        public void HelloGoodbye()

        {

            this.SayHello();

            this.SayGoodbye();

        }

    }

    public class DerivedClass

    {

        public new void SayHello()

        {

            Console.WriteLine("Hi There");

        }

     

     

        public override void SayGoodbye()

        {

            Console.WriteLine("See you later");

        }

    }

     

     ____________________________________________________________________________

    When I instantiate DerivedClass and call SayHello, or SayGoodbye, I get "Hi There" and "See you later". If I call HelloGoodbye, I get "Hello" and "See you later". This is because SayGoodbye is virtual, and can be replaced by derived classes. SayHello is only hidden, so when I call that from my base class I get my original method.

    Thanks

    sankar


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Friday, July 22, 2011 9:31 AM
  • Hello,

    I think, in your example should be

    public class DerivedClass : BaseClass
    {
      ...
    }
    


    Regards

    Adam

    Friday, July 22, 2011 12:31 PM
  • Hi urprob,

    How is it going with our friends' suggestions?

    Have a nice day,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 25, 2011 8:18 AM
    Moderator