优秀的编程知识分享平台

网站首页 > 技术文章 正文

c#,无处不在的委托(c#委托实例)

nanyue 2025-05-08 03:59:42 技术文章 3 ℃

c#委托

委托的定义

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。

什么是委托

首先要知道什么是委托,用最通俗易懂的话来讲,你就可以把委托看成是用来执行方法(函数)的一个东西。

如何使用委托

在使用委托的时候,你可以像对待一个类一样对待它。即先声明,再实例化。只是有点不同,类在实例化之后叫对象或实例,但委托在实例化后仍叫委托。

委托的声明

委托声明决定了可由该委托引用的方法。委托可指向一个与其具有相同标签的方法。使用delegate 关键字修饰。

例如:

public delegate void Test();

上面的委托可被用于引用任何一个无参数的方法,并 且没有返回值。

声明委托的语法

delegate <return type> <delegate-name> <parameter list>

委托的实例化

一旦声明了委托类型,委托对象必须使用 new 关键字来创建,且与一个特定的方法有关。当创建委托时,传递到 new 语句的参数就像方法调用一样书写,但是不带有参数。例如:

public delegate void Test();

Test test=new Test(TestMethod); //完整的写法

Test test1=TestMethod; //简写

委托的调用

test.Invoke();

test1.Invoke();


实例

using System;


namespace weituo

{

public delegate void Test();

class Program

{


public static void TestMethod()

{

Console.WriteLine("进行委托的测试");

}


public static void TestMethod2()

{

Console.WriteLine("进行多播委托的测试");

}


static void Main(string[] args)

{

Console.WriteLine("*******************委托的使用*************************");


Test test = new Test(TestMethod); //委托的实例化

Test test1 = TestMethod; //委托的简写与test效果一样


test.Invoke();

test1.Invoke();


Console.WriteLine("****************多播委托的使用*************************");

Console.WriteLine("++++++++++++++向委托中添加一个方法++++++++++++++++++++");


test += TestMethod2; //向委托中添加一个方法


test += delegate () //向委托中添加一个匿名方法

{

Console.WriteLine("向委托中添加一个匿名方法");

};

test.Invoke();


Console.WriteLine("--------------将委托中的一个方法减去------------------");

test -= TestMethod2; //同样委托中的方法也可以减掉

test.Invoke();


Console.WriteLine("******************测试委托结束***********************");

Console.ReadKey();


}

}

}

运行效果


委托的多播

委托对象可使用 "+" 运算符进行合并。一个合并委托调用它所合并的两个委托。只有相同类型的委托可被合并。"-" 运算符可用于从合并的委托中移除组件委托。

使用委托的这个有用的特点,您可以创建一个委托被调用时要调用的方法的调用列表。这被称为委托的 多播(multicasting),也叫组播。下面的程序演示了委托的多播:

实例:

using System;


namespace weituo

{

public delegate void Test();

class Program

{


public static void TestMethod()

{

Console.WriteLine("进行委托的测试");

}


public static void TestMethod2()

{

Console.WriteLine("进行多播委托的测试");

}


static void Main(string[] args)

{

Console.WriteLine("*******************委托的使用*************************");


Test test = new Test(TestMethod); //委托的实例化

Test test1 = TestMethod; //委托的简写与test效果一样


test.Invoke();

test1.Invoke();


Console.WriteLine("****************多播委托的使用*************************");

Console.WriteLine("++++++++++++++向委托中添加一个方法++++++++++++++++++++");


test += TestMethod2; //向委托中添加一个方法


test += delegate () //向委托中添加一个匿名方法

{

Console.WriteLine("向委托中添加一个匿名方法");

};

test.Invoke();


Console.WriteLine("--------------将委托中的一个方法减去------------------");

test -= TestMethod2; //同样委托中的方法也可以减掉

test.Invoke();


Console.WriteLine("******************测试委托结束***********************");

Console.ReadKey();


}

}

}

运行效果


总结:

  使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C++中的函数指针不同,委托是面向对象,而且是类型安全的。

Tags:

最近发表
标签列表