C#委托学习

控制台应用程序代码

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

namespace MyDelegate
{
    delegate void Del(string msg);

    class Program
    {
        static void Main(string[] args)
        {
            Del handler = ReturnMsg;
            string info = "名叫小白,年方二八,除了没钱和长的太帅外基本没缺点";
            string gift = "夜间服务一辈子";

            MethodWithCallback(info, gift, handler);
            
        }

        public static void ReturnMsg(string msg)
        {
            Console.WriteLine("{0}", msg);
        }

        /// <summary>
        /// 增加一个说媒场景
        /// </summary>
        /// <param name="s1">男孩信息</param>
        /// <param name="s2">所给信物</param>
        /// <param name="callback">媒婆</param>
        public static void MethodWithCallback(string s1, string s2, Del callback)
        {
            StringBuilder say = new StringBuilder();

            say.Append("有个家伙喜欢上你了,他的消息如下:\n");
            say.Append(s1);
            say.Append("\n他还送给你");
            say.Append(s2);
            say.Append("作为定情信物");

            callback(say.ToString());


        }
    }
}
posted on 2013-11-10 12:35  神秘藏宝室  阅读(403)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<