Subscribe to and Unsubscribe from Events

Link: http://msdn.microsoft.com/en-us/library/ms366768(v=vs.80).aspx

Unsubscribe anonymous method in C#

Link: http://stackoverflow.com/questions/183367/unsubscribe-anonymous-method-in-c-sharp

Action myDelegate = delegate(){Console.WriteLine("I did it!");};

MyEvent += myDelegate;

// .... later

MyEvent -= myDelegate;

MyEventHandler foo = null;
foo = delegate(object s, MyEventArgs ev)
    {
        Console.WriteLine("I did it!");
        MyEvent -= foo;
    };
MyEvent += foo;

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: