C#委托的简单使用

1, Action委托

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

namespace Del
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            //Action委托
            Action action = new Action(calculator.Report);
            calculator.Report();
            action.Invoke();
            action();
            

        }
    }

    class Calculator
    {
        public void Report()
        {
            Console.WriteLine("I have 3 methods");
        }

        public int Add(int a, int b)
        {
            int result = a + b;
            return result;
        }

        public int Sub(int a, int b)
        {
            int result = a - b;
            return result;
        }
    }
}


2,  Func<int, int, int> 泛型委托

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

namespace Del
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            //Action委托
            Action action = new Action(calculator.Report);
            calculator.Report();
            action.Invoke();
            action();


            //泛型委托
            Func<int, int, int> func1 = new Func<int, int, int>(calculator.Add);
            //1,2是参数类型,3是函数返回值类型
            Func<int, int, int> func2 = new Func<int, int, int>(calculator.Sub);

            int x = 100;
            int y = 100;
            int z = 0;
            z = func1.Invoke(x, y);
            Console.WriteLine(z);

            z = func1(x, y);
            Console.WriteLine(z);

            z = func2.Invoke(x, y);
            Console.WriteLine(z);

            z = func2(x, y);
            Console.WriteLine(z);
        }
    }

    class Calculator
    {
        public void Report()
        {
            Console.WriteLine("I have 3 methods");
        }

        public int Add(int a, int b)
        {
            int result = a + b;
            return result;
        }

        public int Sub(int a, int b)
        {
            int result = a - b;
            return result;
        }
    }
}

3, 自己申明委托


证明委托是类


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

namespace Del
{
    public delegate double Calc(double x, double y);//委托是类申明时与类平级 申明在命称空间中

    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            Calc calc1 = new Calc(calculator.Add);
            Calc calc2 = new Calc(calculator.Sub);
            Calc calc3 = new Calc(calculator.Div);
            Calc calc4 = new Calc(calculator.Mul);

            double a = 100;
            double b = 200;
            double c = 0;

            c = calc1.Invoke(a, b);
            Console.WriteLine(c);

            c = calc2.Invoke(a, b);
            Console.WriteLine(c);

            c = calc3(a, b);
            Console.WriteLine(c);

            c = calc4(a, b);
            Console.WriteLine(c);

        }
    }

    class Calculator
    {
        public void Report()
        {
            Console.WriteLine("I have 4 methods");
        }

        public double Add(double a, double b)
        {
            double result = a + b;
            return result;
        }

        public double Sub(double a, double b)
        {
            double result = a - b;
            return result;
        }
        public double Mul(double a, double b)
        {
            return a * b;
        }

        public double Div(double a, double b)
        {
            return a/b;
        }
    }
}


猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/80635824