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
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 GetEmptyAction()
{
// Why is this line valid?
// 'delegate {}' has no parameters, but Action 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?