Created
March 14, 2014 10:36
-
-
Save eka808/9545449 to your computer and use it in GitHub Desktop.
Simplest C# event sample ever
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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