Results 1 to 3 of 3

Thread: Virtual function in c#

  1. #1
    Join Date
    Dec 2008
    Posts
    19

    Virtual function in c#

    hi,

    Can anybod briefly explain about "what is virtual function?" and how to write a virtual function in c#.
    Give me one example of writing one virtual function.
    And what is the basic use of that.

    Thanks in Advance.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Virtual function in c#

    Virtual functions implement the concept of polymorphism are the same as in C#, except that you use the override keyword with the virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.

    Code:
    class Shape
    {
        public virtual void Draw()
        {
            Console.WriteLine("Shape.Draw")    ;
        }
    }
    
    class Rectangle : Shape
    
    {
        public override void Draw()
        {
            Console.WriteLine("Rectangle.Draw");
        }
    }

    Virtulal function can not be instatntiated. Declaring a function as virtual tells the compiler that this function is being overrided by the child one's

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Virtual function in c#

    Virtual Keyword force the method to be implemented in the derived class. When we call that method, the method in the drived class will be executed. not method in the base class. We can provide new implementation with help override keyword

Similar Threads

  1. Unable to call overridden virtual function
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 23-02-2010, 06:38 PM
  2. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  3. Pure virtual function call
    By Heather5 in forum Software Development
    Replies: 4
    Last Post: 21-10-2009, 10:54 PM
  4. Winword.exe R6025 - Pure Virtual Function Call
    By Dharuna in forum MS Office Support
    Replies: 2
    Last Post: 09-06-2009, 12:27 AM
  5. How to function daemon Tools as a Virtual CD Burner?
    By Faakhir in forum Windows Software
    Replies: 3
    Last Post: 25-02-2009, 08:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,963,624.55877 seconds with 17 queries