Example #1
0
        private void btnDelegate_Click(object sender, EventArgs e)
        {
            logger.Debug("delegate");
            Counting counting = new Counting();

            //使用CountingAlert這個delegate型別建立新的delegate實體, 並將參考指向Alert這個method
            Counting.CountingNotify del = new Counting.CountingNotify(Alert);
            //將delegate實體加入至Class中同型別的delegate Field中
            counting.notifyHandler += del;
        }
Example #2
0
        private void btnAnonymous_Click(object sender, EventArgs e)
        {
            logger.Debug("anonymous delegate");
            Counting counting = new Counting();

            //使用CountingAlert這個delegate型別建立新的delegate實體, 並將參考指向匿名方法
            Counting.CountingNotify del = delegate(int currentNum)
            {
                int limit = 5;

                logger.DebugFormat("current num: {0}, limit: {1}", currentNum, limit);

                if (currentNum > limit)
                {
                    logger.DebugFormat("currnet num > {0}", limit);
                }
            };
            //將delegate實體加入至Class中同型別的delegate Field中
            counting.notifyHandler += del;
        }