[.Net Concept]理解事件的宣告方式與用法
#region Event
event NameChangingEventHandler NameChanging;
event NameChangedEventHandler NameChanged;
#endregion</pre>
#region Event event EventHandler<FriendEventArgs> FriendAdded; #endregion
#region Protected Method protected void OnFriendAdded(FriendEventArgs e) { if (FriendAdded == null) return; FriendAdded(this, e); } #endregion
#region Public Method public void AddFriend(Person friend) { Friends.Add(friend); OnFriendAdded(new FriendEventArgs(friend.Name)); } #endregion
…
class FriendEventArgs : EventArgs { #region Var private String _name; #endregion
#region Property
public String Name
{
get
{
if (_name == null)
return String.Empty;
return _name;
}
private set
{
_name = value;
}
}
#endregion
#region Constructor
public FriendEventArgs(string name)
{
this.Name = name;
}
#endregion
}
#region Protected Method
protected void OnNameChanging(EventArgs e)
{
if (NameChanging == null)
return;
NameChanging(this, e);
}
protected void OnNameChanged(EventArgs e)
{
if (NameChanged == null)
return;
NameChanged(this, e);
}
#endregion</pre>