委托是用于方法赋值
应用
- UI层和其他层的交互
- 在一些特定的场景中需要将方法注入到其他里面去执行
使用方法
- 定义目标方法
- 定义委托类型 委托和目标方法基本一致
- 申明委托变量
- 变量赋值
- 执行委托
委托多播
using System;
namespace 委托
{
//2 定义委托类型 委托和目标方法基本一致
public delegate void DelegateEat();
class Program
{//1 应该有目标方法
public static void ZSEat()
{
Console.WriteLine("张三吃饭饭--->");
}
public static void LSEat( )
{
Console.WriteLine("李四吃饭饭--->");
}
public static void WWEat( )
{
Console.WriteLine("王五吃饭饭--->");
}
public static void ZLEat( )
{
Console.WriteLine("赵六吃饭饭--->");
}
public static void Main(string[] args)
{
//3 申明委托变量
DelegateEat delegateEat;
//4 赋值
delegateEat = ZSEat;
delegateEat += LSEat;
delegateEat += WWEat;
delegateEat += ZLEat;
//多播
delegateEat += delegateEat;
delegateEat -= LSEat;
//5 执行委托
delegateEat();
}
}
}
委托传递参数
using System;
namespace 委托传递参数
{
public delegate void DelegateEat();//1.定义委托类型
class Program
{
/// <summary>
/// 第一个参数放置符合类型的方法!!!
/// </summary>
/// <param name="de"></param>
/// <param name="userName"></param>
public static void ExcDelegateEat(DelegateEat de,string userName)//2.(传入的目标方法,执行姓名)
{
Console.WriteLine("{0}在使用吃的功能",userName);
de();
}
public static void LSEat()//3.目标方法
{
Console.WriteLine("李四吃呵呵哒");
}
public static void Main(string[] args)
{
ExcDelegateEat(LSEat,"李四");//4.调用ExcDelegateEat传入参数(目标方法,姓名),委托执行
}
}
}
委托变形
代码实现,1,Demo类
using System;
namespace LLWH
{
public class Demo
{
public void HeHe(string userName)
{
Console.WriteLine("say HeHe in class Demo By {0}",userName);
}
}
}
2.主类
using System;
using LLWH;
namespace 委托变形
{
//定义委托类型
public delegate void DelegateOneParStr(string str);
class MyClass
{
public void SayHi( string name)
{
Console.WriteLine("你好世界 by :{0}",name);
}
}
internal class Program
{
public static void Main(string[] args)
{
DelegateOneParStr dele = new MyClass().SayHi;
dele += new Demo().HeHe;
dele("shit");
}
}
}
简单有参数的委托
使用func快捷实现
using System;
namespace 简单有参数的委托
{
internal class Program
{
public static string TestStr()
{
Console.WriteLine("无参数 有返回值");
return string.Empty;
}
public static float TestFloat(int i, double d)
{
Console.WriteLine("两个参数 一个返回值");
return 15.5f;
}
public static void Main(string[] args)
{
//一个代表返回值类型
Func<string> func01 = TestStr;
func01();
Func<int, double, float> func02 = TestFloat;
func02(10, 15.5);
}
}
}
简单无参数的委托,使用action实现
using System;
namespace 简单形式的无参数的委托
{
class Program
{
public static void SayHi()
{
Console.WriteLine("sayHi");
}
public static void SayHiOnePar(int number)
{
Console.WriteLine("SayHiOnePar");
}
public static void Main(string[] args)
{
//委托看的是方法返回值类型和参数类型
// /只要满足该类型的方法都能委托
Action action = SayHi;
action();
Action<int> action01 = SayHiOnePar;
action01(10);
}
}
}
更加简单的委托,通过Lambda表达式实现
using System;
namespace 更加简单的委托
{
internal class Program
{
//public static void Hehe()
// {
// Console.WriteLine("啊 大海");
// Console.WriteLine("啊 我的母亲");
// Console.WriteLine("shit");
// }
public static void Main(string[] args)
{
//Lambda 表达式
Action action = ()//(参数)
=> //固定格式
{// 方法体
Console.WriteLine("啊 大海");
Console.WriteLine("啊 我的母亲");
Console.WriteLine("shit");
};
// Action action = Hehe;
action();//调用委托
}
}
}
Lambda
using System;
namespace Lambda
{
internal class Program
{
public static void Main(string[] args)
{
Action<int, string> action01 = (index, userName) =>
{
Console.WriteLine("index is -->{0}",index);
};
Func<char, int[], string> func = (c, ints) =>
{
Console.WriteLine("over");
return "aaa";
};
action01(10, "xsaxsaxsa");
func('a', new int[10]);
}
}
}