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); }
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); }
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.
A method declared as virtual in the base class can be implemented in the derived class with override keyword. But it is not a must. If we use new keyword it hides the method
The "virtual" keyword is used in a method declaration when the method is intended to be overridden in the derived class, this is used in polymorphism. in this case, when you call this method in a derived class instance that is referred to by a base class reference, then the overridden version of the method will be called.
When you declare a base class method as virtual then you can use the keyword "override" in the derived class version of the method. If you don't use the "override" keyword then you need to use the "new" keyword instead to "hide" the base class version, otherwise the compiler will issue a warning, just to make sure you know what you are doing!