Virtual function in c#

Explain virtual function in c# with an example

Questions by isaacsundarsingh

Editorial / Best Answer

bb.geetha  

  • Member Since May-2008 | Jun 6th, 2008


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.

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}

class Rectangle : Shape

{
public override void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}


Showing Answers 1 - 21 of 21 Answers

bb.geetha

  • Jun 6th, 2008
 

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.

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}

class Rectangle : Shape

{
public override void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}


ynvpavan

  • Oct 1st, 2008
 

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

  Was this answer useful?  Yes

In C#, a virtual function is always considered to be the root of virtual
dispatch; that is, once C# finds a virtual method, it looks no further up the
inheritance hierarchy. If a new virtual Sort( ) function is introduced into
Window, the runtime behavior of ListBox is unchanged.


When ListBox is compiled again, however, the compiler generates a warning:


class1.cs(54,24): warning CS0114: 'ListBox.Sort( )' hides inherited member 'Window.Sort(
)'.


To make the current member override that implementation, add the override
keyword. Otherwise add the new keyword.
To remove the warning, the programmer must indicate what he intends. He can mark
the ListBox Sort( ) method new, to indicate that it is not an override of the
virtual method in Window:


public class ListBox : Window
{
public new virtual void Sort( ) {...}
}


This action removes the warning. If, on the other hand, the programmer does
want to override the method in Window, he need only use the override keyword to
make that intention explicit.

  Was this answer useful?  Yes

kirangiet

  • Sep 10th, 2009
 

Some Facts about Virtual Keyword

1)It is not compulsury to mark the derived/child class function with Override KeyWord while base/parent class contains a virtual method
2)Instead of Virtual we can use New Keyword
3)We will get a warning if we won't use Virtual/New keyword.
4)At the end its all depends which class object we are creating and assigned to which  class reference.

Finally we are using virtual/new just for the sake of compiler.

Trust me write the code your self and test then you won't disagree.

article

  • Feb 10th, 2010
 

1) Virtual functions/Methods implement the concept of polymorphism in C#, using the override keyword with the virtual function implementaion in the child class.

2) The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.

  Was this answer useful?  Yes

What               :           Virtual Function is used to have polymorphic Effect in my  Architecture.

Grammar:                    Base Class Should have Member Function Marked as Virtual

                                    Derived Class Should have Member function marked as override .

 
How:                           Class Base

{

public virtual void MyMethod(){} 

}

Class Derived1:Base

{

public override void MyMethod(){} 

}

 
Class Derived2:Base

{

public override void MyMethod(){} 

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions