GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  ASP.NET
Go To First  |  Previous Question  |  Next Question 
 ASP.NET  |  Question 160 of 164    Print  
Virtual function in c#
explain virtual function in c# with an example


  
Total Answers and Comments: 4 Last Update: September 10, 2009     Asked by: isaacsundarsingh 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: bb.geetha
 
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");
}
}




Above answer was rated as good by the following members:
rajanikanth99488, ponvanaraja, YordanGeorgiev, mukta1902, shounakbasak, Ag2703, Raghvendra.mask
June 06, 2008 02:47:45   #1  
bb.geetha Member Since: May 2008   Contribution: 8    

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.

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

class Rectangle : Shape

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



 
Is this answer useful? Yes | NoAnswer is useful 6   Answer is not useful 0Overall Rating: +6    
October 01, 2008 21:14:11   #2  
ynvpavan Member Since: September 2007   Contribution: 16    

RE: Virtual function in c#
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
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 24, 2009 14:57:18   #3  
mailaditisrivastava Member Since: February 2009   Contribution: 1    

RE: Virtual function in c#

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.


 
Is this answer useful? Yes | No
September 10, 2009 05:22:38   #4  
kirangiet Member Since: June 2009   Contribution: 24    

RE: Virtual function in c#
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape