Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-01-2009
Member
 
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.
Reply With Quote
  #2  
Old 10-01-2009
Member
 
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
Reply With Quote
  #3  
Old 10-01-2009
Member
 
Join Date: May 2008
Posts: 2,008
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:31 PM.