views:

57

answers:

1

When i first started using monotouch i found a page with some code samples for simple and frequent tasks... but on some of those code samples i found some things like this one:

var imageRect = new RectangleF(0f, 0f, 320f, 109f);
using (var myImage = new UIImageView(imageRect))
{  
    myImage.Image = UIImage.FromFile("myImage.png");
    myImage.Opaque = true;
    view.AddSubview(myImage);
}

The UIImageView is created inside a using() block.

I'm a .Net developer and i know what a using() does, but i dont understand why is it used on this example. So my question is if this is the best way of creating views, and what are the differences (if any) of this aproach and creating views without the using() block.

+1  A: 

According to the Monotouch documentation, the C# version of NSObject implements IDisposable so that deterministic destruction is ensured. Likely, the using block is to ensure that the UIImageView is destroyed entirely once it's removed from the view

rpetrich
Thank you very much for your answer :) but wasn't the object inside the using() supposed to be disposed when the using() block ends?
Socram
My best guess would be that Novell has changed `IDisposable` to use reference counting semantics for `NSObject` descendants to conform with the Objective-C pattern.
rpetrich