高级委托使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateTest
{
    /// <summary>
    /// 委托的简单小实例
    /// </summary>
    class Program
    {

        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.Unicode;

            TestDelegate("社会人",MyDelegateImp);

            TestDelegate("实习生", (name) => { Console.WriteLine(name); });

            TestDelegate("在校生", (name) => { Console.WriteLine(name); });
        }

        /// <summary>
        /// 声明委托
        /// </summary>
        /// <param name="name"></param>
        public delegate void DelegateName(string name);

        /// <summary>
        /// 委托方法传递
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static bool DelegateHaveRuturnImp(string str)
        {
            return str =="1";
        }

        /// <summary>
        /// 委托方法实现
        /// </summary>
        /// <param name="str"></param>
        static void MyDelegateImp(string str)
        {
            Console.WriteLine(str);
        }

        static void TestDelegate(string name,DelegateName myDelegate)
        {
            myDelegate(name);
        }

        /// <summary>
        /// 匿名委托
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        static void TestAction(string name,Action<string> action)
        {
            action(name);
        }
    }
}
posted @ 2020-04-21 10:33  LY-CS  阅读(99)  评论(0编辑  收藏  举报