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

前端技术 2023/09/06 C#
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;
    }
  }
}

本文地址:https://www.stayed.cn/item/15793

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。