CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS MICROSOFT C# DETAILS
Question :
What does the keyword virtual mean in the method definition?


Category C# Interview Questions
Rating (4.8) By 31 users
Added on 7/28/2006
Views 2440
Rate it!
Answers:

If a base class have a virtual method it means that method must be override by the derived class using override keyword.



A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes
or its called do nothing function .



 Posted by: Ajay Verma    

Contact Ajay Verma  Contact Ajay Verma

virtual keyword is used in the base class method
when derived class use virtual method we use override key with derived class method and the implementation of method can be changed but not affecting the type signature of base class.

eg: base class
public virtual method1()
{
int a,b,c;
c=a+b;
Console.WriteLine(c);
}

derived class

public override method1()
{
int a,b,c;
c=a*b;
Console.WriteLine(c);
}




 Posted by: ranjeeth    

Contact ranjeeth  Contact ranjeeth

virtual meathod is define in base class and which is over-ridden in child class..
base:class
public virtual method1()
{
string c="virtual method1"
Console.WriteLine(c);
}

derived class

public override method1()
{
string c="override meathod"
Console.WriteLine(c);
}



 Posted by: rahil mohd    

Contact rahil mohd  Contact rahil mohd

If a Method is declared as Virutval in Base class then you can override the Method in derived Class by using Override keyword.

class Program
{
static void Main(string[] args)
{
CA a;
a= new CB();
a.Foo();
}

}
class CA
{
public virtual void Foo()
{
Console.WriteLine("Foo in parent Class");
}
}
class CB : CA
{

public override void Foo()
{
Console.WriteLine("Foo in Child Class");
}
}



 Posted by: Nageswara Rao    

Contact Nageswara Rao  Contact Nageswara Rao

Basically virtual method has drastical affect on performance of application. No Doubt we can do all the things without virtual function but if we use Virtual then we have following benefits.

1. Same of name of function can be used in derived class after overriding and can change its body there but with same signature

2. Overrided function will point to same memory location that is of the virtual function. means same memory location can be shared.



 Posted by: Gurpreet Singh    

Contact Gurpreet Singh  Contact Gurpreet Singh


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
How's method overriding different from overloading?

View Answer
What's the top .NET class that everything is derived from?

View Answer
Describe the accessibility modifier protected internal.

View Answer
Are private class-level variables inherited?

View Answer
When you inherit a protected class-level variable? Who is it available to?

View Answer
Does C# support multiple inheritance?

View Answer
How do you inherit from a class in C#?

View Answer
What's the implicit name of the parameter that gets passed into the class' set method?

View Answer
Is it mandatory to implement all the methods which are there in abstract class if we inherit that abstract class..?

View Answer


What is the difference between const and static read-only?



View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C# Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/9101/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.73