欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-29 17:37:46
using system; namespace delegatedemo { class program { private dele...
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;
    }
  }
}