Cannot convert method group '' to non-delegate type 'System.Delegate'. Did you intend to invoke the method?

c#, delegates

Solution

You really shouldn't ever use the type `Delegate` to store a delegate. You should be using a specific type of delegate.

In almost all cases you can use `Action` or `Func` as your delegate type. In this case, `Action` is appropriate:

class Program
{
    static void Test()
    {

    }

    static void Main(string[] args)
    {
        Action action = Test;

        action();
    }
}

You can technically get an instance of `Delegate` by doing this:

Delegate d = (Action)Test;

But actually using a `Delegate`, as opposed to an actual specific type of delegate, such as `Action`, will be hard, since the compiler will no longer know what the signature of the method is, so it doesn't know what parameters should be passed to it.

Problem

I'm trying to store a function reference in a Delegate type for later use. Here's what I'm doing: ``` class Program { static void Test() { } static void Main(string[] args) { Delegate t= (Delegate)Test; } } ``` In this I'm getting following error: Cannot convert method group 'Test' to non-delegate type 'System.Delegate'. Did you intend to invoke the method? Why is this happening?

Original source

Related problems