Event:
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.
Example:
Explanation:
Here DialogClosed is a delegate and OnDialogClosed is an event.
Scenario : We want to show a dialog form where user can enter some data and after that he can save data. When data has been saved then a closed event fire from Dialog to notify caller that I have closed.
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.
Example:
class Program { static void Main(string[] args) { var dialogForm = new DialogForm(); dialogForm.OnDialogClosed += new DialogForm.DialogClosed(dialogForm_OnDialogClosed); Console.WriteLine("Dialog is shown."); dialogForm.ShowDialog(); Console.WriteLine("Dialog data is saved."); dialogForm.SaveData(); Console.ReadLine(); } static void dialogForm_OnDialogClosed(object sender) { //Take action after closed dialog Console.WriteLine("Dialog is closed."); } } class DialogForm { public delegate void DialogClosed(object sender); public event DialogClosed OnDialogClosed; public void ShowDialog() { } public void SaveData() { if (OnDialogClosed != null) OnDialogClosed(this); } }
Explanation:
Here DialogClosed is a delegate and OnDialogClosed is an event.
Scenario : We want to show a dialog form where user can enter some data and after that he can save data. When data has been saved then a closed event fire from Dialog to notify caller that I have closed.