Skip to content

Instantly share code, notes, and snippets.

@solkar
Last active July 13, 2017 06:45
Show Gist options
  • Save solkar/0ab5c135039c572df646664f764678b4 to your computer and use it in GitHub Desktop.
Save solkar/0ab5c135039c572df646664f764678b4 to your computer and use it in GitHub Desktop.
Subscribing for events using lambda expressions
public class EventClassWithoutEvent
{
public Action OnChange { get; set; }
public void Raise()
{
if (OnChange != null)
{
OnChange();
}
}
}
public partial class Program
{
private static void CreateAndRaiseEvent()
{
EventClassWithoutEvent ev = new EventClassWithoutEvent();
ev.OnChange += () =>
Console.WriteLine("1st: Event raised");
ev.OnChange += () =>
Console.WriteLine("2nd: Event raised");
ev.Raise();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment