C#.NET — About the delegate declaration below, which statements are correct? delegate void del(int i); Consider: 1) Declaring the delegate creates a class named del. 2) The signature of del need not match the method's signature. 3) The generated del class derives from MulticastDelegate. 4) The method invoked by del must not be static. 5) The del class contains a one-argument constructor and an Invoke() method.

Difficulty: Easy

Correct Answer: 1, 3 and 5 only

Explanation:


Introduction / Context:
C# delegates are type-safe function pointers. The compiler generates a sealed class behind the scenes for each delegate type. Understanding this generation helps reason about compatibility and invocation.



Given Data / Assumptions:

  • Delegate declaration: delegate void del(int i);
  • We evaluate five statements about what the compiler emits and what methods a delegate can target.


Concept / Approach:
Declaring a delegate produces a class with the same name that derives from MulticastDelegate. Methods assigned to that delegate must be signature-compatible (parameter list and return type). Delegates can reference static or instance methods. The generated delegate type exposes a constructor that takes the target (object for instance methods or null for static) and a method pointer, and it provides an Invoke method matching the delegate signature.



Step-by-Step Solution:

1) True — a compiler-generated class named del appears.2) False — signatures must be compatible to assign/subscribe.3) True — all C# delegate types derive from MulticastDelegate.4) False — both static and instance methods can be targets.5) True — the delegate exposes a specific constructor and an Invoke method.


Verification / Alternative check:
Use reflection at runtime (typeof(del)) to see its BaseType (MulticastDelegate) and methods (Invoke, BeginInvoke/EndInvoke in older patterns).



Why Other Options Are Wrong:

  • A includes statement 2 which is false.
  • C/D include false claims about signatures or static methods.
  • E is incorrect because 2 and 4 are false.


Common Pitfalls:
Assuming any method can be assigned to a delegate regardless of signature; in C#, type safety is enforced.



Final Answer:
1, 3 and 5 only

Discussion & Comments

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