GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Microsoft  >  ASP.NET

 Print  |  
Question:  Virtual function in c#

Answer: explain virtual function in c# with an example


April 04, 2009 14:57:18 #3
 mailaditisrivastava   Member Since: February 2009    Total Comments: 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.

     

 

Back To Question