xiaolizi007
2/20/2017 - 2:06 PM

Delegate Is CallBack Function

Delegate Is CallBack Function #csharp

    [ComVisible(true)]
    public class EventArgs
    {
        //
        // 摘要:
        //     Provides a value to use with events that do not have event data.
        public static readonly EventArgs Empty;

        //
        // 摘要:
        //     Initializes a new instance of the System.EventArgs class.
        public EventArgs();
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateIsEventSignature
{
    class Program
    {

        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.DoSpeakEvents += SlowSpeak;
            p1.DoSpeakEvents += LoudlySpeak;
            p1.DoIt();
            Console.ReadKey();
        }
        static void SlowSpeak(object a, EventArgs e)
        {
            Console.WriteLine("asdfasdfasdf    "+a.ToString());
        }
        static void LoudlySpeak(object a,EventArgs e)
        {
            Console.WriteLine("i am louderly speak   " + a.ToString());
        }

    }
    public class Person
    {
        public delegate void Speak(object o, EventArgs e);
        public event Speak DoSpeakEvents;
        //可以写成下面的这种格式,就不用写delegate了,简写模式
        public EventHandler<EventArgs> TestSpeak;
        //DoIt相当于Nofity,通知所有的挂载函数
        public void DoIt()
        {
            DoSpeakEvents(this, EventArgs.Empty);
        }
    }

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

namespace DelegateIsCallBackFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.Talk(SlowSpeak);
            Console.ReadKey();
        }
        static void SlowSpeak(int i)
        {
            Console.WriteLine(i+" i am slowly speak"+i);
        }
    }
    public class Person
    {
        public delegate void Speak(int i);
        public void Talk(Speak speak)
        {
            for (int i = 0; i < 50; i++)
            {
                speak(i);
            }
        }
    }
}