Action myDelegate = delegate(){Console.WriteLine("I did it!");};
MyEvent += myDelegate;
// .... later
MyEvent -= myDelegate;
Just keep a reference to the delegate around.
More Related Contents:
- delegate keyword vs. lambda notation
- How do I Unregister ‘anonymous’ event handler [duplicate]
- Discrete Anonymous methods sharing a class?
- Invoke(Delegate)
- When & why to use delegates? [duplicate]
- C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’
- Passing variable between winforms
- Why can’t an anonymous method be assigned to var?
- Func vs. Action vs. Predicate [duplicate]
- Super-simple example of C# observer/observable with delegates
- Performance of calling delegates vs methods
- Creating delegates manually vs using Action/Func delegates
- Assigning events in object initializer
- C# pattern to prevent an event handler hooked twice [duplicate]
- Difference between Delegate.Invoke and Delegate()
- How to pass an event to a method?
- MethodInvoker vs Action for Control.BeginInvoke
- Are C# events synchronous?
- Why doesn’t the C# ternary operator work with delegates?
- Why does asynchronous delegate method require calling EndInvoke?
- Func delegate with ref variable
- How can I clear event subscriptions in C#?
- What are the advantages of delegates? [duplicate]
- What does “a field initializer cannot reference non static fields” mean in C#?
- Events – naming convention and style
- Return a value from an Event — is there a Good Practice for this?
- C# – How can I “overload” a delegate?
- Can someone distill into proper English what a delegate is?
- Declaring Func dynamically
- Is it possible to have a delegate as attribute parameter?