委托类型 (delegate type) 表示对具有特定参数列表和返回类型的方法的引用。通过委托,我们能够将方法作为实体赋值给变量和作为参数传递。委托类似于在其他某些语言中的函数指针的概念,但是与函数指针不同,委托是面向对象的,并且是类型安全的。
将一个方法作为参数传递给另一个方法。
委托所指向的函数必须跟委托具有相同的签名。(参数和返回值类型 )
using System;
namespace _143_委托引入
{
public delegate void DelSayHi(string name);
class Program
{
static void Main(string[] args)
{
DelSayHi del = SayHiChinese; // new DelSayHi(SayHiChinese);
del("微咲");
Test("wexiao", SayHiEnglish);
//匿名函数
DelSayHi ndel =
delegate (string name)
{
Console.WriteLine("我是匿名函数" + name);
};
ndel("hello one");
//lambda表达式 => goes to ;;;;;;;;;;
DelSayHi ndel2 = (string name) => { Console.WriteLine(name + "lambda表达式"); };
ndel2("lambda");
Console.ReadKey();
}
public static void Test(string name, DelSayHi del)
{
//调用
del(name);
}
public static void SayHiChinese(string name)
{
Console.WriteLine("你好" + name);
}
public static void SayHiEnglish(string name)
{
Console.WriteLine("Hello" + name);
}
}
}
using System;
namespace _142_为什么使用委托
{
public delegate string DelProStr(string name);
class Program
{
static void Main(string[] args)
{
string[] names = { "aBcDEFg", "HijKLmNOPq", "RxYZ" };
//ProStrToUpper(names);
//roStrSYH(names);
ProStr(names, ProStrToLower);
//匿名函数
ProStr(names, delegate (string name)
{
return name.ToUpper();
}
);
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
//将一个方法作为参数传入方法==委托
public static void ProStr(string[] names,DelProStr del)
{
for (int i = 0; i < names.Length; i++)
{
names[i] = del(names[i]);
}
}
public static string ProStrToUpper(string name)
{
return name.ToUpper();
}
public static string ProStrToLower(string name)
{
return name.ToLower();
}
public static string ProStrSYH(string name)
{
return "\"" + name + "\"";
}
//public static void ProStrToUpper(string[] names)
//{
// for (int i = 0; i < names.Length; i++)
// {
// names[i] = names[i].ToUpper();
// }
//}
//public static void ProStrToLower(string[] names)
//{
// for (int i = 0; i < names.Length; i++)
// {
// names[i] = names[i].ToLower();
// }
//}
//public static void ProStrSYH(string[] names)
//{
// for (int i = 0; i < names.Length; i++)
// {
// names[i] = "\"" + names[i] + "\"";
// }
//}
}
}
委托练习:
求任意数组的最大值
using System;
namespace _144_使用委托求任意数组最大值
{
//声明一个委托:比较o1与o2大小
public delegate int DelCompare(object o1, object o2);
class Program
{
static void Main(string[] args)
{
object[] o = { 1, 2, 3, 4, 5 };
object result = GetMax(o, CompareInt);
Console.WriteLine(result);
object[] o1 = { "dwadawd", "dwaudhwauf", "我是你亲爱的老父亲咿呀咿呀哟咿呀咿呀哟", "wduhwauifgauibgysahjdn" };
result = GetMax(o1, CompareString);
Console.WriteLine(result);
Console.ReadKey();
}
/// <summary>
/// 求数组最大值
/// </summary>
/// <param name="nums">要求最大值的数组</param>
/// <param name="del">比较最大值和数组元素的委托</param>
/// <returns>最大值</returns>
public static object GetMax(object[] nums, DelCompare del)
{
object max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
//要传一个比较的方法, 如果max<nums[i]就会执行if
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
/// <summary>
/// 比较int型数据的大小
/// </summary>
/// <param name="o1"></param>
/// <param name="o2"></param>
/// <returns></returns>
public static int CompareInt(object o1, object o2)
{
int n1 = (int)o1;
int n2 = (int)o2;
return n1 - n2;
}
/// <summary>
/// 比较string型数据的大小
/// </summary>
/// <param name="o1"></param>
/// <param name="o2"></param>
/// <returns></returns>
public static int CompareString(object o1, object o2)
{
string s1 = (string)o1;
string s2 = (string)o2;
return s1.Length - s2.Length;
}
}
}
使用泛型委托求任意数组的最大值
using System;
namespace _145_泛型委托求数组最大值
{
//声明一个委托:比较o1与o2大小
public delegate int DelCompare<T>(T t1,T t2);
//public delegate int DelCompare(object o1, object o2);
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
int max= GetMax<int>(nums, CompareInt);
Console.WriteLine(max);
string[] names = { "微咲", "我是你爹", "sduahwuifhwauibfwabndhwabdywaubfy" };
string maxLenth = GetMax<string>(names, CompareString);
Console.WriteLine(maxLenth);
Console.ReadKey();
}
/// <summary>
/// 求数组最大值
/// </summary>
/// <param name="nums">要求最大值的数组</param>
/// <param name="del">比较最大值和数组元素的委托</param>
/// <returns>最大值</returns>
public static T GetMax<T>(T[] nums, DelCompare<T> del)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
//要传一个比较的方法, 如果max<nums[i]就会执行if
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
/// <summary>
/// 比较int型数据的大小
/// </summary>
/// <param name="n1"></param>
/// <param name="n2"></param>
/// <returns></returns>
public static int CompareInt(int n1, int n2)
{
return n1 - n2;
}
/// <summary>
/// 比较string型数据的大小
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static int CompareString(string s1, string s2)
{
return s1.Length - s2.Length;
}
}
}