博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 委托
阅读量:5855 次
发布时间:2019-06-19

本文共 7892 字,大约阅读时间需要 26 分钟。

 

 

 

委托学习要点:

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;        }    }}

 

转载于:https://www.cnblogs.com/youguess/p/8120067.html

你可能感兴趣的文章
Eclipse代码自动提示
查看>>
对象内存布局 (12)
查看>>
java.io.ByteArrayOutputStream 源码分析
查看>>
你可能没注意的CSS单位
查看>>
35岁之前,你该知道的事(转)
查看>>
【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式修改...
查看>>
sqlite的Top筛选
查看>>
SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
查看>>
如何在ios中集成微信登录功能
查看>>
微软职位内部推荐-Sr SDE
查看>>
设计带有placeHolder的TextView
查看>>
什么是WEBserver? 经常使用的WEBserver有哪些?
查看>>
spring @component的作用
查看>>
2014年,daliu_it 年末文章汇总清单
查看>>
ubuntu12.04上搭建darwin streaming server6.03
查看>>
【高德地图API】汇润做爱地图技术大揭秘
查看>>
Windows系统版本号判定那些事儿
查看>>
【HighCharts系列教程】二、Highcharts结构及API文档
查看>>
Android 自定义对话框使用静态Handler传递参数
查看>>
Hql 执行CRUD
查看>>