What is Virtual Method in C#?
What is Virtual Method in C#?
A virtual method in C# is a method that can be overridden in derived classes. It allows for polymorphism, which is the ability of objects of different classes to respond to the same method call in different ways.
Here are the key points about virtual methods in C#:
To define a virtual method in a base class, you use the virtual
keyword followed by the method signature:
public virtual returnType MethodName(parameters)
{
// method implementation
}
In a derived class, you can override the virtual method using the override
keyword:
public override returnType MethodName(parameters)
{
// overridden method implementation
}
When a virtual method is called on an object, the runtime determines which implementation of the method to execute based on the actual type of the...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào