C# 委托复习
今天复习一下委托,由于很长时间没有使用和复习委托,乍一看完全什么也不记得了,脑子里一点影响也没有了。于是看了看前面委托的文章和传智的视频。才慢慢迷迷糊糊的想起来一点。敲敲代码就记起来了大概了,看来还是要多复习和多敲代码才行。
使用委托直接调用和使用方法调用委托调用方法:
如果方法的签名和委托签名一样可以传值。
using System;
namespace 委托复习
{
public delegate void DelSay(string name); //声明委托类型
class Program
{
static void Main(string[] args)
{
DelSay d = new DelSay(SayC);//声明委托对象
//DelSay d = SayC; 声明委托对象 语句简写
Console.WriteLine("————使用委托直接调用————");
d("张三");//委托调用
Console.WriteLine("————使用方法调用委托调用————");
Test("Tom",SayE);//通过调用方法 调用委托。参数为,字符串 和 方法
Console.ReadKey();
}
public static void Test(string name,DelSay del)//方法,参数为 字符串 和委托。委托可以简写 DelSay d = SayC; 可以直接传方法
{
del(name);//调用委托
}
public static void SayC(string name)//中文打招呼 方法
{
Console.WriteLine("你好!"+name);//输出
}
public static void SayE(string name)//英文打招呼 方法
{
Console.WriteLine("Hello!"+name);//输出
}
}
}生成结果:
匿名方法:
using System;
namespace 匿名
{
public delegate string DelProStr(string name); //声明委托类型
class Program
{
static void Main(string[] args)
{
string[] names = {"ABCdEfgh","IjklMnOPq","RSTuvwXYz" };//字符串数组
ProStr(names,delegate(string name) //方法,匿名方法
{
return name.ToUpper();//转为大写字母
//return name.ToLower();//转为小写字母
//return "\"" + name + "\"";//转为加 双引号
});
for (int i = 0; i < names.Length; i++)//循环输出
{
Console.WriteLine(names[i]);//输出
}
Console.ReadKey();
}
public static void ProStr(string[] name,DelProStr del)//方法 参数:字符串数组 和 委托
{
for (int i = 0; i < name.Length; i++)//循环
{
name[i] = del(name[i]);//调用委托里面的方法
}
}
}
}生成结果:
泛型委托:
using System;
namespace 泛型委托
{
class Program
{
public delegate int DelCompare<T>(T t1, T T2);//声明委托类型(泛型)
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 5, 6, 8, 9, 5, 1, 2, 3, 6, 5, 18, 8, 9, 2 };//整型数组
int maxInt = GetMax<int>(nums, Compare1);//调用方法 参数:整型数组 和 Compare1方法
Console.WriteLine("整型数组最大值:" + maxInt);//输出
string[] strs = {"sadaqw","12e32wfwe32","dsfwq","dgdf25e","sdwq23" };//字符串数组
string maxStr = GetMax<string>(strs, delegate (string s1, string s2)//调用方法 参数:字符串数组 和 匿名委托
{
return s1.Length - s2.Length;//返回 字符串长度 相减结果
});
Console.WriteLine("字符串数组最大值:" + maxStr);//输出
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)//调用 委托方法 判断 返回值是否小于0
{
max = nums[i];//赋值
}
}
return max;//返回
}
public static int Compare1(int n1, int n2)//整型最大值判断 方法
{
return n1 - n2;//返回整型 数组相减结果
}
}
}生成结果:
使用委托窗体传值:
窗体一:
using System;
using System.Windows.Forms;
namespace 窗体传值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//按钮单击事件
{
Form2 f2 = new Form2(showLabel);//实例化 窗体二 传入方法
f2.Show();//打开窗体二
}
public void showLabel(string str)//方法
{
label1.Text = str;//标签显示
}
}
}窗体二:
using System;
using System.Windows.Forms;
namespace 窗体传值
{
public delegate void Del(string str);//委托类型
public partial class Form2 : Form
{
public Del _del;//委托字段
public Form2(Del del)//使用委托接收方法
{
this._del = del;//赋值方法
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//按钮单击事件
{
this._del(textBox1.Text);//调用委托方法。
}
}
}生成结果:
