Câu hỏi phỏng vấn C#
Câu hỏi

What is the difference between overloading and overriding?

Câu trả lời

Here is the difference between method overloading and method overriding in C#:

Method Overloading

  • Overloading allows you to define multiple methods with the same name but different parameters within the same class.
  • The methods must differ in the number, types, or order of parameters.
  • Overloaded methods are resolved at compile-time based on the arguments passed to the method call.
  • Overloading allows you to provide multiple implementations of a method to handle different scenarios.
  • Overloading is a compile-time polymorphism feature.

Example:

csharp Copy
public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
}

Method Overriding

  • Overriding allows you to provide a specific implementation of a method that is already defined in the base class.
  • The method in the derived class mu...
middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What is enum in C#?

senior

What interface should your data structure implement to make the Where method work?

senior

What is Marshalling and why do we need it?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào