Q:
What is a Virtual Method in C#?
belongs to collection: C# Interview Questions and Answers,You Need To Know
C# Interview Questions and Answers,You Need To Know
- What is C#?
- What is an object in c#?
- What are C# attributes and their significance?
- What is the meaning of instantiation in c#?
- How will you differentiate between a Class and a Struct in c#?
- What is the difference between public, static, and void in c#?
- What’s a multicast delegate in c#?
- How do I calculate someone’s age in C#?
- What is the difference between public static, public and static methods in c#?
- What is a Virtual Method in C#?
- List down the fundamental OOP concepts in c#?
- Compare Virtual methods and Abstract methods in c#
- What are Namespaces in C#?
- Is every abstract function virtual in C#, in general?
- What are I/O classes in C#? Define some of the most commonly used ones
- What is the difference between SessionState and ViewState in c#?
- What’s the difference between a method and a function in c#?
- What is the difference between an abstract function and a virtual function in c#?
- What is an interface class? Give one example of it
- What is the advantage of interface class?
- Explain the process of inheriting a class into another class?
- What is the difference between an interface and abstract class?
- What are circular references?
- What is the advantage of abstract class?
- What happens if the inherited interfaces have conflicting method names?
- What is Constructor in C#?
- Explain some points related to the constructor?
- What is difference between “is” and “as” operators in c#?
- Why can’t you specify the accessibility modifier for methods inside the interface?
- What are Value types and Reference types in C#?
- What are Jagged Arrays in c#?
- Is elements of jagged array must be initialized before its use
- Why Doesn’t C# Allow Static Methods to Implement an Interface?
- What do you understand by regular expressions in C#? Write a program that searches a string using regular expressions
- What is the difference between ref & out parameters in c#?
- What is the difference between var and dynamic in C#
- What is the use of ‘using’ statement in C#?
- What is the major use of ‘using’ keyword in c#?
- What is the difference between String and string in C#?
- What is function overloading in c#?
- Explain some ways of doing overloading function in C#
- What is serialization?
- Explain Inheritance in C# with an example?
- What is the best way to give a C# auto-property an initial value?
- List down the reason behind the usage of C# language
- What are Custom Exceptions?
- What is Managed or Unmanaged Code in c#?
- Explain the features of C#?
- What is the difference between constant and readonly in C#?
- Can we use “this” command within a static method?
- Explain Deadlock?
- illustrate Race Condition?
- What is Thread Pooling?
- Distinguish between finally and finalize blocks?
- What is Boxing and Unboxing in C#?
- What is enum in C#?
- Describe Accessibility Modifiers in C#
- What is the difference between ‘protected’ and ‘protected internal’?
- How do short-circuited operators work?
- What is the “volatile” keyword used for?
- Why do we use Async and Await in C#?
- Explain different states of a thread in C#?
- What are delegates?
- What is the difference between “continue” and “break” statements in C#?
- What can you tell us about the XSD file in C#?
- What are Custom Control and User Control?
- What are sealed classes in C#?
- What is the difference between Array and Arraylist?
- Can a private virtual method can be overridden?
- What are Properties in C#?
- What are accessors?
- What are the differences between System.String and System.Text.StringBuilder classes?
- Why Properties are introduced in C#?
- What is the difference between the dispose and finalize methods in C#?
- What are partial classes?
- What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ?
- What are the advantages of partial classes?
- What is the difference between late binding and early binding in C#?
- What are the differences between IEnumerable and IQueryable?
- What is Reflection in C#?
- Give an example of removing an element from the queue?
- What is the difference between directcast and ctype?
- How to implement a singleton design pattern in C#?
- What is the difference between the “throw” and “throw ex” in .NET?
- List down the commonly used types of exceptions in .net?
- How can we sort the elements of the Array in descending order?
- What is a Hashtable in C#?
- What is Multithreading with .NET?
A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived the class. It is used when a method’s basic functionality is the same but sometimes more functionality is needed in the derived class. A virtual method is created in the base class that can be overridden in the derived class. We create a virtual method in the base class using the virtual keyword and that method is overridden in the derived class using the override keyword.
When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. The overriding method also provides more than one form for a method. Hence, it is also an example of polymorphism.
When a method is declared as a virtual method in a base class and that method has the same definition in a derived class then there is no need to override it in the derived class. But when a virtual method has a different definition in the base class and the derived class then there is a need to override it in the derived class.
When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member if no derived class has overridden the member.
Virtual Method: