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