With OS X 10.6, blocks were introduced in Objective-C. Blocks are similar to anonymous functions, delegates or lambda expressions in .NET.

Many functions and methods uses blocks; Monobjc provides a way to map anonymous functions, delegates or lamda expressions so they can be used as blocks.

The block creation/disposal is performed by the API wrapper:

// Define the lambda expression for sorting
Func sorter = delegate(Id id1, Id id2) {
        return id1.SendMessage("compare:", id2);
};
NSArray sorted = array.SortedArrayUsingComparator(sorter); // Block is created and disposed in the method.

Or for the asynchronous case:

// Register the block, and store the result. It will be used later in de-registration.
this.listener = NSNotificationCenter.DefaultCenter.AddObserverForNameObjectQueueUsingBlock(NSApplication. NSApplicationWillBecomeActiveNotification, null, null, block);

// ...

NSNotificationCenter.DefaultCenter.RemoveObserver(this.listener);

Note

Usually, the API wrapper takes care of creation/disposal of the blocks so you don't need to care about it.