委托学习要点:
1.为什么要使用委托
将一个方法作为参数传递给另一个方法.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 为什么使用委托{ public delegate void DelProStr(string [] names); public delegate string DelProStr2(string name); class Program { static void Main(string[] args) { //三个需求 //1.将一个字符串数组中每个元素都转换成大写 //2.将一个字符串数组中每个元素都转换成小写 //3.将一个字符串数组中每个元素两边都加上 双引号 string[] names = { "abCDefG", "HIJKlmnOP", "QRsTuW", "XyZ" }; //Test(names, ProStrToUpper); //ProStr(names, ToSYH); //匿名函数 ProStr(names, delegate(string name) { return name.ToLower(); }); for (int i = 0; i < names.Length; i++) { Console.WriteLine(names[i]); } Console.ReadKey(); } #region public static void ProStr(string [] names,DelProStr2 del2) { for(int i=0;i
2.委托的概念
声明一个委托类型
委托所指向的函数必须跟委托具有相同的签名(参数和返回值)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _08委托概念{ //声明一个委托指向一个函数 public delegate void DelSayHi(string name); class Program { static void Main(string[] args) { //DelSayHi del = new DelSayHi(SayChinese); DelSayHi del = SayChinese; //Test("张三",); Test("张三", SayChinese); Test("alexx", SayEnglish); Console.ReadKey(); } public static void Test(string name,DelSayHi del) { //调用 del(name); } public static void SayChinese(string name) { Console.WriteLine("吃了么?"+name); } public static void SayEnglish(string name) { Console.WriteLine("Nice to meet you"+name); } }}
3.匿名函数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _09匿名函数{ public delegate void DelSay(string name); class Program { static void Main(string[] args) { //匿名函数 DelSay del = delegate(string name) { Console.WriteLine("Hello"+name); }; del("alexx"); //lamda表达式 => goes to DelSay del = (string name) => { Console.WriteLine("HI "+name); }; del("alexx"); Console.ReadKey(); } //public static void }}
4.练习:使用委托求数组的最大值
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _10求数组的最大值{ public delegate int DelCompare(object o1,object o2); class Program { static void Main(string[] args) { object [] o ={ "1","2","3","4","56"}; //DelCompare del = Compare; //object resule = GetMax(o, Compare2); object result = GetMax(o, delegate(object o1, object o2) { string s1 = (string)o1; string s2 = (string)o2; return s1.Length - s2.Length; }); Console.WriteLine(result); Console.ReadKey(); } public static object GetMax(object [] nums,DelCompare del) { object max = nums[0]; for(int i=0;i
5.练习:使用委托求任意数组的最大值 =>泛型委托
6.泛型委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _11泛型委托{ public delegate int DelCompare(T o1, T o2); class Program { static void Main(string[] args) { int [] nums = { 1,2,3,4,5,123 }; int max = GetMax (nums, delegate(int o1, int o2) { return o1 - o2; }); Console.WriteLine(max); Console.ReadKey(); } public static T GetMax (T[] nums, DelCompare del) { T max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (del(max, nums[i]) < 0) { max = nums[i]; } } return max; } }}
7.多播委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 多播委托{ public delegate void DelTest(string str); class Program { static void Main(string[] args) { // DelTest del = t1; del += t2; del += t3; del("aa"); Console.WriteLine(del); Console.ReadLine(); } private static void t1(string name) { Console.WriteLine("t1"+name); } private static void t2(string name) { Console.WriteLine("t2"+name); } private static void t3(string name) { Console.WriteLine("t3"+name); } }}
8.lamda表达式
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _12lamda表达式{ public delegate void Delone(); public delegate void Deltwo(string name); public delegate string Delthree(string name); class Program { static void Main(string[] args) { Delone one = delegate() { }; Delone two = () => { }; Deltwo a = delegate(string name) { }; Deltwo b = (string name) => { }; Delthree ac = delegate(string name) { return name; }; Delthree ab =(string name)=>{ return name;}; List list = new List () { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; list.RemoveAll(n => n >= 8);//lamda表达式 foreach(var item in list) { Console.WriteLine(item); } Console.ReadKey(); } }}
9.使用委托来实现窗体传值.
(父窗体与子窗体来回传值)
FORM1:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.dellaber = laberShow; frm2.Show(); //this.Hide(); } private void laberShow(string str) { this.label1.Text = str; } private void button2_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Textbox1 = this.label1.Text + "ZQ!"; frm2.Show(); } }}
FORM2:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public delegate void DelShowForm1Laber(string str); public partial class Form2 : Form { public DelShowForm1Laber dellaber; public Form2() { InitializeComponent(); } private string _textbox1; public string Textbox1 { get { return _textbox1; } set { _textbox1 = value; this.label1.Text = _textbox1; } } private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = this.textBox1.Text.Trim(); //fuzhi //Form1 frm1 = new Form1(); //frm1.Show(); dellaber(this.textBox1.Text); this.Hide(); } private void form2_laber(string str) { this.label1.Text = str; } }}