C#.NET delegates — Which of the following statements are correct?

Difficulty: Easy

Correct Answer: If signatures of two methods are same they can be called through the same delegate object.

Explanation:


Introduction / Context:
Delegates in C#.NET are type-safe function pointers that encapsulate a method reference and, optionally, a target object. This question tests what delegates can and cannot invoke, and the importance of method signatures for delegate compatibility.



Given Data / Assumptions:

  • A delegate can reference static or instance methods.
  • Delegate compatibility is based on matching signature (parameter types, order, and return type).
  • Delegates can be multicast (invoke multiple compatible methods).
  • Variable argument methods are supported when the delegate itself uses a matching params signature.


Concept / Approach:
The core rule is signature compatibility: a single delegate type can call any method whose signature matches the delegate’s signature. Static and instance targets are both supported. Claims that categorically deny static or instance method support are incorrect. Variable argument methods are possible if the delegate type is declared with a suitable params parameter.



Step-by-Step Solution:

Identify the true statement: methods with the same signature can be invoked through one delegate type.Eliminate false statements: delegates work with static and instance methods; variable arguments are supported through matching delegate signatures.


Verification / Alternative check:
Create delegate int Op(int x, int y); and point it to multiple methods Add and Subtract; both are callable if the signatures match.



Why Other Options Are Wrong:

  • Option A: Delegates can call static methods (target object is null internally).
  • Option B: Delegates can represent methods with params when declared accordingly.
  • Option D: Delegates can call instance methods (they capture a target object).


Common Pitfalls:
Assuming delegates ignore return type or that they can freely coerce mismatched signatures—C# requires type safety.



Final Answer:
If signatures of two methods are same they can be called through the same delegate object.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion