首页 > 编程 > C# > 正文

C#中使用委托的3种方式代码示例

2020-01-24 02:04:17
字体:
来源:转载
供稿:网友
using System;namespace DelegateDemo{  class Program  {    private delegate int Cacu(string str);    static void Main(string[] args)    {      //1      Cacu cacu = new Cacu(CacuInstance);      Console.WriteLine(cacu("Hello,Wrold"));      //2      Cacu cacu1 = new Cacu(delegate(string str) { return str.Length; });      Console.WriteLine(cacu1("Hello,Wrold"));      //3      Cacu cacu2 = new Cacu((str) => { return str.Length; });      Console.WriteLine(cacu2("Hello,Wrold"));    }    static int CacuInstance(string str)    {      return str.Length;    }  }}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表