C#委托学习
控制台应用程序代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyDelegate { delegate void Del(string msg); class Program { static void Main(string[] args) { Del handler = ReturnMsg; string info = "名叫小白,年方二八,除了没钱和长的太帅外基本没缺点"; string gift = "夜间服务一辈子"; MethodWithCallback(info, gift, handler); } public static void ReturnMsg(string msg) { Console.WriteLine("{0}", msg); } /// <summary> /// 增加一个说媒场景 /// </summary> /// <param name="s1">男孩信息</param> /// <param name="s2">所给信物</param> /// <param name="callback">媒婆</param> public static void MethodWithCallback(string s1, string s2, Del callback) { StringBuilder say = new StringBuilder(); say.Append("有个家伙喜欢上你了,他的消息如下:\n"); say.Append(s1); say.Append("\n他还送给你"); say.Append(s2); say.Append("作为定情信物"); callback(say.ToString()); } } }
文章对您有帮助,开心可以打赏我,金额随意,欢迎来赏!
需要电子方面开发板/传感器/模块等硬件可以到我的淘宝店逛逛