Why does my delegate not work when passing as a method parameter?

I created a delegate and I want to use a delegate as a parameter in the method parameter list. I think I want to call a handler, as in the Main method, which works fine.

Question: How to pass a delegate to a method?

The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)     <--Write Here!!!
        {
            d.handler("33");
        }


        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");    <--Like this example here (these work)
            p.handler("DisneyLand");
            p.handler("Cattle Wars");

            testDel(p.handler("Cattle Wars");
        }
    }
}

List of errors:

Error   1   The best overloaded method match for 'ConsoleApplication1.Program.testDel(ConsoleApplication1.Program.Del)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  12  ConsoleApplication1
Error   2   Argument 1: cannot convert from 'void' to 'ConsoleApplication1.Program.Del' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  20  ConsoleApplication1
Error   3   ) expected  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  44  ConsoleApplication1

Work code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)
        {
            d.Invoke("L");
        }

        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");
            p.handler("DisneyLand");
            p.handler("Cattle Wars");
            p.testDel(p.handler);
        }
    }
}
+4
source share
2 answers

In line

testDel(p.handler("Cattle Wars"));

p.handler. In testDelno more Del. You may have searched for:

testDel(p.handler);

It transmits Del, so it testDelcan cause it.

+6
source

You must pass the delegate to the method with the "Cattle Wars" parameter

   testDel(p.handler);

Then you should look like this.

    public void testDel(Del d)
    {
        d.Invoke("L");
    }

, . , , . , .

    public delegate void Del(string e);
    Del handler = DelegateMethod;

, void , .

   Del handler = DelegateMethod;

, , , void .

    handler = BaseBall;


     public void BaseBall(string a) 
     {
         //code here
     }

, , .

+1

Source: https://habr.com/ru/post/1537112/


All Articles