Skip to content

Instantly share code, notes, and snippets.

@airbreather
Created September 12, 2017 19:18
Show Gist options
  • Save airbreather/fb012c59f31143eaf30ed260e275ece5 to your computer and use it in GitHub Desktop.
Save airbreather/fb012c59f31143eaf30ed260e275ece5 to your computer and use it in GitHub Desktop.
class A
{
public event EventHandler Foo;
public void RaiseFoo() => this.Foo?.Invoke(this, EventArgs.Empty);
}
class B
{
public int Val;
public void Subscribe(A a) => a.Foo += this.Handler;
public void Handler(object sender, EventArgs args)
{
((A)sender).Foo -= this.Handler;
Console.WriteLine("B{0} Handled", this.Val);
}
}
A a = new A();
B b1 = new B { Val = 1 };
B b2 = new B { Val = 2 };
B b3 = new B { Val = 3 };
b1.Subscribe(a);
a.Foo -= b3.Handler;
b2.Subscribe(a);
a.RaiseFoo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment