Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
What is meant by Virtual Keyword in C#?
Posted 7-Apr-11 18:49pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What is the virtual keyword used for?
 
Virtual - If a base class method is to be overriden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding).
Note that the class member method may be overriden even if the virtual keyword is not used, but its usage makes the code more transparent & meaningful. In VB.NET, we may use the overridable keyword for this purpose.
 
When the override keyword is used to override the virtual method, in a scenario where the base class method is required in a child class along with the overriden method, then the base keyword may be used to access the parent class member. The following code example will make the usage more clear.
 
public class Employee
{
  public virtual void SetBasic(float money) //This method may be overriden
  { Basic += money; }
}
 

public class Manager : Employee
{
  public override void SetBasic(float money) //This method is being overriden
  {
   float managerIncentive = 10000;
   base.SetSalary(money + managerIncentive); //Calling base class method
  }
}
  Permalink  
Comments
Abhinav S at 8-Apr-11 0:56am
   
Good explanation. My 5.
mahen25 at 8-Apr-11 1:01am
   
Thanks
Abhinav S at 8-Apr-11 1:02am
   
You are welcome.
SwitcherSoft at 8-Apr-11 2:42am
   
yes
Member 9829004 at 3-Apr-13 1:51am
   
thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To put in one line (if you are familiar with OOPS concepts and subclassing) -
abstract - Must be overriden
virtual - can be overriden
sealed - cannot be overriden
  Permalink  
Comments
Albin Abel at 8-Apr-11 2:28am
   
It rules. :) My 5. "if you are familiar with OOPS concepts and subclassing"= OP is learning Oops from your answer. He would be appreciate your helps. :)
Abhinav S at 8-Apr-11 6:07am
   
Thank you.
nK0de at 14-Jul-12 1:56am
   
I didn't know about the 'sealed' keyword! Thank you.
mustankit at 8-Jun-13 5:56am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
 
If you have some doubts in these links you may specifically ask here. We will be glad to answer.
 
http://msdn.microsoft.com/en-us/library/9fkccyh4%28v=vs.71%29.aspx[^]
 
http://www.akadia.com/services/dotnet_polymorphism.html[^]
 
cheers
  Permalink  
Comments
Abhinav S at 8-Apr-11 1:01am
   
Good links. 5.
Albin Abel at 8-Apr-11 2:23am
   
Thanks Abhinav
dilip0165 at 22-Jun-13 2:38am
   
second one is good link . Thanks a lot ......
Member 8406857 at 12-Feb-14 1:56am
   
Apart from your overiding concept where we use this Virtual keyword?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 199
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,972
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100