Virtual functions implement the concept of polymorphism are the same as in C#, except that you use the override keyword with the virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
class Shape { publicvirtualvoid Draw() { Console.WriteLine("Shape.Draw") ; } }
Virtual functions implement the concept of polymorphism are the same as in C#, except that you use the override keyword with the virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
class Shape { publicvirtualvoid Draw() { Console.WriteLine("Shape.Draw") ; } }
Virtulal function can not be instatntiated. Declaring a function as virtual tells the compiler that this function is being overrided by the child one's
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.
1)It is not compulsury to mark the derived/child class function with Override KeyWord while base/parent class contains a virtual method 2)Instead of Virtual we can use New Keyword 3)We will get a warning if we won't use Virtual/New keyword. 4)At the end its all depends which class object we are creating and assigned to which class reference.
Finally we are using virtual/new just for the sake of compiler.
Trust me write the code your self and test then you won't disagree.
1) Virtual functions/Methods implement the concept of polymorphism in C#, using the override keyword with the virtual function implementaion in the child class.
2) The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
Virtual function in c#
Profile Answers by isaacsundarsingh Questions by isaacsundarsingh
Questions by isaacsundarsingh
Editorial / Best Answer
bb.geethaProfile Answers by bb.geetha Questions by bb.geetha
Virtual functions implement the concept of polymorphism are the same as in C#, except that you use the override keyword with the virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
Related Answered Questions
Related Open Questions