Click here to Skip to main content
11,928,352 members (54,406 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
What is meant by Virtual Keyword in C#?
Posted 7-Apr-11 19:49pm
Rate this: bad
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
Albin Abel 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 8-Apr-11 6:07am
Thank you.
Selva Ganesh 27-Jan-15 7:58am
Selva Ganesh 27-Jan-15 7:59am
nandrigal pala
nK0de 14-Jul-12 1:56am
I didn't know about the 'sealed' keyword! Thank you.
mustankit 8-Jun-13 5:56am
Rate this: bad
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
Abhinav S 8-Apr-11 0:56am
Good explanation. My 5.
mahen25 8-Apr-11 1:01am
Abhinav S 8-Apr-11 1:02am
You are welcome.
SwitcherSoft 8-Apr-11 2:42am
Member 9829004 3-Apr-13 1:51am
rod7 2-Apr-15 21:20pm
Why are Basic and SetSalary not defined anywhere?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


If you have some doubts in these links you may specifically ask here. We will be glad to answer.[^][^]

Abhinav S 8-Apr-11 1:01am
Good links. 5.
Albin Abel 8-Apr-11 2:23am
Thanks Abhinav
dilip0165 22-Jun-13 2:38am
second one is good link . Thanks a lot ......
Member 8406857 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 8 Apr 2011
Copyright © CodeProject, 1999-2015
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