C#事件示例程序

C#事件机制示例
本文提供了一个使用C#编写的事件机制示例程序。该程序定义了一个名为CalculateWithEvent的类,它触发了事件,并有一个EventListener类订阅这些事件。通过具体的加法和减法操作演示了事件的触发和响应过程。
C#事件示例程序
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace MyCalculate dot.gif{
InBlock.gif
InBlock.gif    
public delegate void CalculateEventHandler(object sender,EventArgs e);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class CalculateWithEvent dot.gif{
InBlock.gif        
public event CalculateEventHandler Calculate;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
protected virtual void OnCalculate(EventArgs e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (Calculate != nulldot.gif{
InBlock.gif                Calculate(
this, e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int Add(int a, int b) dot.gif{
InBlock.gif            OnCalculate(EventArgs.Empty);
InBlock.gif            
return a + b;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int Sub(int a, int b) dot.gif{
InBlock.gif            OnCalculate(EventArgs.Empty);
InBlock.gif            
return a - b;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class EventListener dot.gif{
InBlock.gif        
private CalculateWithEvent CalculateCase;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public EventListener(CalculateWithEvent calculateCase)dot.gif{
InBlock.gif            CalculateCase 
= calculateCase;
InBlock.gif            CalculateCase.Calculate 
+= new CalculateEventHandler(CalculateCase_Calculate);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void CalculateCase_Calculate(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//throw new Exception("The method or operation is not implemented.");
InBlock.gif
            Console.WriteLine("运算事件被调用!");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif 
ExpandedBlockEnd.gif}

None.gif
None.gif
namespace TestNS
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using MyCalculate;
InBlock.gif
InBlock.gif    
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CalculateWithEvent myCalculate 
= new CalculateWithEvent();
InBlock.gif            EventListener myListener 
= new EventListener(myCalculate);
InBlock.gif            myCalculate.Add(
23);
InBlock.gif            myCalculate.Sub(
32);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

虚拟的,用于在基类定义一个方法,以便在继承类中重写方法(override)

转载于:https://www.cnblogs.com/coderdream/archive/2007/04/13/712078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值