1、为什么要使用委托?
将一个方法作为参数传递给另一个方法、
2、委托概念
声明一个委托类型,写在命名空间下
委托所指向的函数必须跟委托具有相同的签名(参数and返回值)。
3、匿名函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public delegate void DelSayHi(string name);
class Program
{
static void Main(string[] args)
{
//第一种
//DelSayHi del = delegate (string name)
// {
// Console.WriteLine("你好" + name);
// };
//del("张三");
//第二种:lamda表达式 => gose to 去哪
DelSayHi del = (string name) => { Console.WriteLine("你好" + name); };
del("张三");
Console.ReadKey();
}
}
}
练习:求任意数组的最大值
4、泛型委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
public delegate int DelCompare<T>(T t1, T t2);
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6 };
int max = GetMax<int>(nums, Compare1);
Console.WriteLine(max);
string[] names = { "fs", "fsd", "dfsdf" };
string max1 = GetMax<string>(names, (string s1, string s2) =>
{
return s1.Length - s2.Length;
});
Console.WriteLine(max1);
Console.ReadKey();
}
public static T GetMax<T>(T[] nums, DelCompare<T> del)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
//传一个比较的方法
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
public static int Compare1(int n1, int n2)
{
return n1 - n2;
}
}
}
5、lamda表达式
using System;
usi ng System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public delegate void DelOne();
public delegate void DelTwo(string name);
public delegate string DelThree(string name);
class Program
{
static void Main(string[] args)
{
DelOne del = () => { };//delegate(){};
DelTwo del2 = delegate (string name) { }; //delegate(string name){};
DelThree del3 = (string name) => { return name; };//delegate(string name){};
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//移除4后面的数字
list.RemoveAll(n => n > 4);
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
6、窗体传值
1)创建两个窗体form1和form2
form1
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(ShowMsg);
frm2.Show();
}
void ShowMsg(string str)
{
label1.Text = str;
}
}
}
窗体二
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 WindowsFormsApp1
{
//声明一个委托
public delegate void DelTest(string str);
public partial class Form2 : Form
{
public DelTest _del;
public Form2(DelTest del)
{
this._del = del;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_del(textBox1.Text);
}
}
}
7、多播委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public delegate void DelTest();
class Program
{
static void Main(string[] args)
{
//委托可以指向多个函数
DelTest del = T1;
del += T1;
del += T2;
del += T3;
del += T4;
del -= T3;
del -= T1;
del();
Console.ReadKey();
}
public static void T1()
{
Console.WriteLine("我是T1");
}
public static void T2()
{
Console.WriteLine("我是T2");
}
public static void T3()
{
Console.WriteLine("我是T3");
}
public static void T4()
{
Console.WriteLine("我是T4");
}
}
}