Difficulty: Easy
Correct Answer: public static sample operator + (sample a, sample b)
Explanation:
Introduction / Context:
Operator overloading in C# uses a very specific signature. Getting the modifiers and placement right is essential to compile and achieve the intended behavior.
Given Data / Assumptions:
Concept / Approach:
All overloaded operator methods must be public static and must return the containing type (or another appropriate return type, depending on the operator). They cannot be abstract or instance methods. The method name is the keyword 'operator' followed by the operator symbol.
Step-by-Step Solution:
Verification / Alternative check:
Attempt to compile A–C and note the compiler errors. D compiles and can be exercised with 'var s = a + b;'
Why Other Options Are Wrong:
They violate the 'public static' requirement or misuse 'abstract' for operators.
Common Pitfalls:
Forgetting 'static', returning the wrong type, or mismatching parameter types.
Final Answer:
public static sample operator + (sample a, sample b)
Discussion & Comments