C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
2
Reply
What is Virtual Method in C#?
Yachit Kumar
13y
3k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
A virtual method can be redefined. In the C# language, the virtual keyword designates a method that can be overridden in derived classes. This enables you to add new, derived types without modifying the rest of your program. The runtime type of objects thus determines what your program does. Keywords Example NoteThis program introduces two classes, A and B. Class A has a public virtual method called Test. Class B, meanwhile, derives from class A and it provides a public override method called Test as well.The virtual modifier tells the compiler that when any class derived from class A (such as class B) is used, an override method should be used instead of the virtual method. Override MethodProgram that introduces virtual method [C#]using System;class A {public virtual void Test(){Console.WriteLine("A.Test");} }class B : A {public override void Test(){Console.WriteLine("B.Test");} }class Program {static void Main(){// Compile-time type is A.// Runtime type is A as well.A ref1 = new A();ref1.Test();// Compile-time type is A.// Runtime type is B.A ref2 = new B();ref2.Test();} }OutputA.Test B.Test
Dinesh Maurya
13y
0
The virtual modifier tells the compiler that when any class derived from class A (such as class B) is used, an override method should be used instead of the virtual method.
Yogesh Sharma
13y
0
What is Abstract method and how different from Virtual Method?
What is the process of Serialization?
Message