Skip to content

Instantly share code, notes, and snippets.

@eka808
Created March 14, 2014 10:36
Show Gist options
  • Save eka808/9545449 to your computer and use it in GitHub Desktop.
Save eka808/9545449 to your computer and use it in GitHub Desktop.
Simplest C# event sample ever
void Main()
{
new Foo().DoSomething();
}
// Define other methods and classes here
class Foo
{
public Foo()
{
// Define the event
this.SomethingHappened += HandleEvent;
this.SomethingHappened += (sender, args) => Console.WriteLine("(As a delegate) Something happened to " + sender);
}
public event EventHandler SomethingHappened;
public void DoSomething()
{
EventHandler handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public void HandleEvent(object sender, EventArgs args)
{
Console.WriteLine("(As a raw method) Something happened to " + sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment