一、委托的概念
C# 中的委托(Delegate)是一种引用类型,是把方法变成一种引用类型的方式。
委托适用于实现事件和回调方法,所有的委托都派生自 System.Delegate 类。关键字是delegate
使用委托和使用类一样,都有定义和实例化两步
在实例化委托时,可以将委托的实例与具有相同返回值类型的方法相关联,这样就可以通过委托来调用方法。另外,使用委托还可以将方法作为参数传递给其他方法,
二、委托的声明与调用
1. 使用委托来调用方法
using System;
namespace zzz
{
internal class Program
{
static void Main(string[] args)
{
//创建自定义类的对象
People people = new People();
//创建个无参无返回值的委托对象
Delegate1 delegate1 = new Delegate1(people.PeopleMethod);
delegate1();
//创建个有参数无返回值的委托对象
Delegate2 delegate2 = new Delegate2(people.PeopleMethod2);
delegate2(10);
//创建个有参数有返回值的委托对象
Delegate5 delegate3 = new Delegate3(people.PeopleMethod5);
delegate5("abc", "def");
}
}
//自定义的类
class People
{
//自定义个无返回值无参数的方法
public void PeopleMethod()
{
Console.WriteLine(1);
}
//自定义个有参数无返回值的方法
public void PeopleMethod2(int a)
{
Console.WriteLine(a);
}
//自定义个有参数有返回值的方法
public string PeopleMethod3(string a,string b)
{
return a+b;
}
}
//定义个无返回值无参数的委托
delegate void Delegate1();
//定义个无返回值有一个参数的委托
delegate void Delegate2(int a);
//定义个有返回值有参数的委托
delegate string Delegate3(string a,string b);
}
2.使用委托作为 函数参数
using System;
namespace zzz
{
internal class Program
{
static void Main(string[] args)
{
//创建自定义类的对象
People people = new People();
//委托对象
Delegate1 delegate1 = new Delegate1(people.Method);
//使用委托作为 函数参数
people.Method(delegate1);
}
}
//自定义的类
class People
{
public void Method(Delegate1 del)
{
del();
}
}
//定义了一个无返回值无参数的委托
delegate void Delegate1();
}
3.定义泛型委托
using System;
namespace zzz
{
internal class Program
{
static void Main(string[] args)
{
//创建自定义类的对象
People people = new People();
DFanXing<string, int, float> delegateFanXing = new DFanXing<string, int, float>(people.MethodFanXing);
string str = delegateFanXing(10,10.5f);
Console.WriteLine(str);
}
}
//自定义的类
class People
{
public string MethodFanXing(int a,float b)
{
return (a + b).ToString();
}
}
//定义个有返回值有类型参数的委托
delegate T DFanXing<T,W,V>(W a,V b);
}
4. 委托作为字段时,建议用Action和Func
Action和Func简单来说有返回值就用Func,无返回值就用Action
using System;
namespace zzz
{
internal class Program
{
//当字段使用
static Action action; //无返回值方法模板
static Action<int> action2; //无返回值有一个参数方法模板
static Func<int> func; //有返回值(int类型)方法模板
static Func<int,string> func2; //最后一个类型代表返回值
//使用Action和Func委托
static void Main(string[] args)
{
action = Method1;
action();
action2 = Method2;
action2(200);
func = Method3;
func();
func2 = Method4;
func2(3);
Console.ReadKey();
}
public static void Method1()
{
Console.WriteLine(1);
}
public static void Method2(int a)
{
Console.WriteLine(a);
}
public static int Method3()
{
return 100;
}
public static string Method4(int a)
{
return a.ToString();
}
}
}
5. 多播
委托对象有一个非常有用的属性,那就是可以通过使用+运算符将多个对象分配给一个委托实例,同时还可以使用-运算符从委托中移除已分配的对象,当委托被调用时会依次调用列表中的委托
using System;
namespace zzz
{
internal class Program
{
Action action;
Func<int> func;
static void Main(string[] args)
{
Program program = new Program();
//多播
program.action = Method1;
program.action += Method2;
program.action -= Method1;
program.action();
//多播具有返回值的,只会得到最后一个方法的结果
program.func = Method3;
program.func += Method5;
Console.WriteLine(program.func());
Console.ReadKey();
}
public static void Method1()
{
Console.WriteLine(1);
}
public static void Method2()
{
Console.WriteLine(2);
}
public static int Method3()
{
return 10;
}
public static int Method4()
{
return 20;
}
}
}
文章介绍了C#中的委托概念,它是一种引用类型,用于实现事件和回调。文中详细展示了如何声明和调用委托,包括无参无返回值、有参数无返回值、有参数有返回值的方法,并且提到了使用委托作为函数参数和定义泛型委托的情况。此外,文章还讨论了Action和Func的使用以及委托的多播特性。

5582

被折叠的 条评论
为什么被折叠?



