C#委托语句

/ / C#委托语句

C#委托类似于 C 或C+中指向函数的指针,委托是保存对方法的引用类型变量,可以在运行时更改引用。

委托特别用于实现事件和回调方法,所有委托都隐式派生自System.Delegate类。

声明委托

委托声明确定委托可以引用的方法,委托可以引用与委托具有相同签名的方法。

public delegate int MyDelegate (string s);

前面的委托可用于引用具有单个string参数并返回int类型变量的任何方法。

委托声明的语法为-

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

初始化委托

声明委托类型后,必须使用new关键字创建委托对象并与特定方法关联。创建委托时,传递给new表达式的参数编写与方法调用类似但没有方法的参数。

public delegate void printString(string s);
...
printString ps1=new printString(WriteToScreen);
printString ps2=new printString(WriteToFile);

下面的示例演示了委托的声明、实例化和使用该委托可用于引用接受整数参数并返回整数值的方法。

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl {
   
   class TestDelegate {
      static int num = 10;
      
      public static int AddNum(int p) {
         num += p;
         return num;
      }
      public static int MultNum(int q) {
         num *= q;
         return num;
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
         //create delegate instances
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         
         //使用委托对象调用方法
         nc1(25);
         Console.WriteLine("Value of Num: {0}", getNum());
         nc2(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

Value of Num: 35
Value of Num: 175

委托组播

可以使用“ +”运算符来组成委托对象,只能组成相同类型的代表。“-”运算符可用于从组合委托中删除组件委托。

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-delegates.html

来源:LearnFk无涯教程网

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl {
   class TestDelegate {
      static int num = 10;
      
      public static int AddNum(int p) {
         num += p;
         return num;
      }
      public static int MultNum(int q) {
         num *= q;
         return num;
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
         //create delegate instances
         NumberChanger nc;
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         
         nc = nc1;
         nc += nc2;
         
         //calling multicast
         nc(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

Value of Num: 75

使用委托

下面的示例演示了委托的用法,委托printString可用于引用接受字符串作为输入但不返回任何内容的方法。

无涯教程使用此委托调用两个方法,第一个方法将字符串打印到控制台,第二个方法将其打印到文件-

using System;
using System.IO;

namespace DelegateAppl {

   class PrintString {
      static FileStream fs;
      static StreamWriter sw;
      
      //delegate declaration
      public delegate void printString(string s);

      //此方法打印到控制台
      public static void WriteToScreen(string str) {
         Console.WriteLine("The String is: {0}", str);
      }
      
      //此方法打印到文件
      public static void WriteToFile(string s) {
         fs = new FileStream("c:\\message.txt",
         FileMode.Append, FileAccess.Write);
         sw = new StreamWriter(fs);
         sw.WriteLine(s);
         sw.Flush();
         sw.Close();
         fs.Close();
      }
      
      //此方法将委托作为参数并将其用于根据需要调用方法
      public static void sendString(printString ps) {
         ps("Hello World");
      }
      
      static void Main(string[] args) {
         printString ps1 = new printString(WriteToScreen);
         printString ps2 = new printString(WriteToFile);
         sendString(ps1);
         sendString(ps2);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

The String is: Hello World

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

AI PPT创作实战课 -〔小6〕

AI原生应用入门课 -〔李梦冉/程默〕

深入拆解消息队列47讲 -〔许文强〕

零基础学Python(2023版) -〔尹会生〕

深入浅出可观测性 -〔翁一磊〕

编程高手必学的内存知识 -〔海纳〕

操作系统实战45讲 -〔彭东〕

技术管理案例课 -〔许健〕

分布式协议与算法实战 -〔韩健〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者