C S h a r p 委 托 之 类 间 互 调 函 数 CSharp委托之类间互调函数 CSharp

Utils.cs

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

namespace Test4DeletegateBetweenMultiClasses
{
    class Utils
    {
        public static int Add(int a ,int b)
        {
            return a + b;
        }

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


    }
}

Form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test4DeletegateBetweenMultiClasses
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Utils utils = new Utils();
        
        public delegate int TestDelegate(int a, int b);
        TestDelegate testDele = Utils.Add;
        


        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(testDele(300, 200).ToString());

            testDele += utils.Sub;

            MessageBox.Show(testDele(300, 200).ToString());


        }
    }
}