委托

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

namespace 委托练习
{
    //定义委托
    public delegate void AddDelegate(int a, int b);
    class Class1
    {
       
        static void Main(string[] args)
        {
            // 创建委托实例
            int a = 3, b = 4;
            AddDelegate add = new AddDelegate(Add);
            add(a, b);

            Console.ReadKey();
        }
        //把相关参数传给方法
        public static void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }

    }
}

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

namespace 委托练习
{
public class 无聊练练
{
static void Main(string[] args)
{
//写一个Action这个泛型集合(say是下面的静态方法)
Action<string> test = new Action<string>(say);
//调用函数
test("这个是下面的Str");
Console.ReadKey();
}
//定义这个静态方法,方便上面的调用,感觉这个就像是一样的,果然有共同性
public static void say(string str)
{
Console.WriteLine(str);
}
}
}

原文地址:https://www.cnblogs.com/yaodengfeng/p/7753859.html