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

 更新时间:2015年03月31日 08:53:51   投稿:junjie  
这篇文章主要介绍了C#中使用委托的3种方式代码示例,本文直接给出代码实例,没有相关的说明解释,需要的朋友可以参考下
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;
    }
  }
}

相关文章

最新评论