Bird
Raised Fist0

Identify the problem in this code:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Interfaces

Identify the problem in this code:

interface ICalculator {
  int Add(int a, int b);
}
class Calculator : ICalculator {
  public int Add(int x, int y) {
    return x + y;
  }
  public int Subtract(int a, int b) {
    return a - b;
  }
}
ACalculator must implement Subtract in interface
BNo problem, code is correct
CAdd method parameters names must match interface
DInterface cannot have methods with parameters
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature matching

    Parameter names can differ; only types and order must match. Add(int x, int y) matches Add(int a, int b).
  2. Step 2: Check interface implementation rules

    Calculator implements Add method as required. Subtract is extra and allowed.
  3. Final Answer:

    No problem, code is correct -> Option B
  4. Quick Check:

    Parameter names can differ, types must match [OK]
Quick Trick: Parameter names can differ; types and order must match [OK]
Common Mistakes:
MISTAKES
  • Thinking parameter names must match
  • Expecting all methods in class to be in interface
  • Misunderstanding interface method rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes