2012年11月4日 星期日

The simplest event and delegate in c#

This is the simplest code sample in c#.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DelegateAndEvent
{
    public partial class Form1 : Form
    {
        public delegate void callback();

        public event callback callbackEvent  = null;
        public callback callbackEvent2 = null;

        public Form1()
        {
            InitializeComponent();
            this.callbackEvent += new callback(Cabllback);
            callbackEvent2 = new callback(Cabllback);
        }

        public void Cabllback()
        {
            System.Console.WriteLine("test");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (callbackEvent != null)
                callbackEvent();
            if (callbackEvent2 != null)
                callbackEvent2();
        }
    }
}

沒有留言: