Why can a parameter-less anonymous method be assigned to a delegate Action that expects a parameter?

1 day ago 7
ARTICLE AD BOX

I'm learning about delegates in C# and came across a pattern that I don't fully understand. I have a generic method that returns an Action<T>, which I expect to be a method that accepts one parameter of type T.

However, it seems I can return an anonymous method with no parameters (delegate {}) without any compiler errors.

Here is a minimal example:

using System; public class Program { public static Action<T> GetEmptyAction<T>() { // Why is this line valid? // 'delegate {}' has no parameters, but Action<T> expects one. return delegate { }; } }

My question is:

What C# language rule or feature allows a method with fewer parameters (in this case, zero) to be assigned to a delegate type that expects more parameters?

I've seen the lambda expression _ => {} used for this, which makes it clearer that the parameter is being ignored. Is the delegate {} syntax working based on the same principle of delegate compatibility?

Read Entire Article