zoukankan      html  css  js  c++  java
  • 委托小例子两个

    示例一:

    class Program

        {

    static void Main(string[] args)

            {

    string res= new Program().ShowMsg();

    Console.WriteLine(res);

    Console.Read();

            }

    private delegate string DelegateMsg();

    private string ShowMsg()

            {

    DelegateMsg dmsg = GetMsg;

    return dmsg.Invoke();

            }

    public string GetMsg()

            {

    return DateTime.Now.Ticks.ToString();

            }

        }

    示例二:

    class Program

        {

    static void Main(string[] args)

            {

    var res = new Program().Show();

    Console.WriteLine(res);

    Console.Read();

            }

    public string Show()

            {

    MsgService msgService = new MsgService();

                msgService.delegateMsg += new MsgService.DelegateMsg(msgService.ShowMsg);

                msgService.delegateMsg += new MsgService.DelegateMsg(msgService.GetMsg);

    if (msgService.delegateMsg != null)

                {

                    msgService.delegateMsg.Invoke();

                }

    return "";

            }

    public class MsgService

            {

    public delegate string DelegateMsg();

    public DelegateMsg delegateMsg;

    public string ShowMsg()

                {

    DelegateMsg dmsg = GetMsg;

    var res = dmsg.Invoke();

    Console.WriteLine(res);

    return res;

                }

    public string GetMsg()

                {

    var res = DateTime.Now.Ticks.ToString();

    Console.WriteLine(res);

    return res;

                }

            }

        }

  • 相关阅读:
    ECSHOP热门搜索关键词随机显示
    最全面的常用正则表达式大全
    ToString()、Convert.ToString()、(string)、as string 的区别
    RabbitMQ Server的安装、配置及常用命令
    RabbitMQ 学习资料
    使用dynamic来简化反射实现
    C#4.0中的dynamic关键字和ExpandoObject对象
    Erlang
    RestSharp使用备忘
    mysql group by的特殊性
  • 原文地址:https://www.cnblogs.com/jzb-dev/p/3484495.html
Copyright © 2011-2022 走看看