C#委托之类间互调函数
原创
©著作权归作者所有:来自51CTO博客作者茗君(Major_S)的原创作品,请联系作者获取转载授权,否则将追究法律责任
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());
}
}
}