ch5_3.cs
来自「《c#技术内幕代码》」· CS 代码 · 共 51 行
CS
51 行
using System;
public class EventTestClass
{
// The value to track
private int nValue;
// Allow a handler for the event
public delegate void ValueChangedEventHandler();
// This is the event itself
public event ValueChangedEventHandler Changed;
// This method is used to fire the event
protected virtual void OnChanged()
{
if (Changed != null)
Changed();
else
Console.WriteLine("Event fired. No handler!");
}
public EventTestClass(int nValue)
{
SetValue( nValue );
}
public void SetValue( int nV )
{
if ( nValue != nV )
{
nValue = nV;
// Fire the event
OnChanged();
}
}
}
public class CH5_3
{
public static void Main()
{
EventTestClass etc = new EventTestClass(3);
etc.SetValue(5);
etc.SetValue(5);
etc.SetValue(3);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?