码迷,mamicode.com
首页 > Windows程序 > 详细

C# 事件

时间:2016-06-12 12:16:50      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace CSharpStudy
{


    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            CalculatorManager calculatorManager = new CalculatorManager();
            calculator.MyCalculate += calculatorManager.Add;
            calculator.Calculate(2100);
            calculator.MyCalculate += calculatorManager.Substract;
            calculator.Calculate(100100);
            Console.ReadKey();
        }

    }
    public class Calculator
    {
        public delegate void CalculateEventhandler(object sender, CalculateEventArgs e);
        public event CalculateEventhandler MyCalculate;
        public virtual void OnCalculate(CalculateEventArgs e)
        {
            if (MyCalculate != null)
            {
                MyCalculate(this, e);
            }
        }
        public void Calculate(int x, int y)
        {
            CalculateEventArgs e = new CalculateEventArgs(x, y);
            OnCalculate(e);
        }
        public class CalculateEventArgs : EventArgs
        {

            public readonly int x, y;

            public CalculateEventArgs(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

        }
    }
    public class CalculatorManager
    {

        public void Add(object sender, Calculator.CalculateEventArgs e)
        {
            Console.WriteLine(e.x + "+" + e.y);
        }
        public void Substract(object sender, Calculator.CalculateEventArgs e)
        {

            Console.WriteLine(e.x + "-" + e.y);
        }


    }

}

C# 事件

标签:

原文地址:http://www.cnblogs.com/shidengyun/p/5576933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!