Difficulty: Medium
Correct Answer: delegate void del(int i, float j); del d; Sample s = new Sample(); d = new del(ref s.MyFun); d(10, 1.1f);
Explanation:
Introduction / Context:
This tests how to correctly declare and use delegates to invoke a method with specific parameters in C#. Correct signature matching is crucial for delegates to work.
Given Data / Assumptions:
Concept / Approach:
Delegates must match the method signature exactly. MyFun takes two parameters: int and float. Therefore, the delegate must be declared as delegate void del(int, float).
Step-by-Step Solution:
Verification / Alternative check:
Compile and run with Option B, works correctly, printing the message.
Why Other Options Are Wrong:
Mismatched signatures or invalid syntax.
Common Pitfalls:
Forgetting that delegate parameters must exactly match target method parameters; confusing declaration and instantiation syntax.
Final Answer:
delegate void del(int i, float j); del d; Sample s = new Sample(); d = new del(ref s.MyFun); d(10, 1.1f);
Discussion & Comments