C#1到C#4使用委托的几种方式


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;
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM