Search code examples
c#objective-ccocoansautoreleasepool

Does using(){...} in C# serve the same purpose as autoreleasepool{...} in Cocoa?


I found out about using(){} in C# --> Uses of "using" in C#

I know that autorelease{} is not the same as using(){} because cocoa uses ARC and C# uses GC. --> Is it necessary to use autoreleasepool in a Swift program?

I just want to confirm from somebody who has used both if they do in fact serve the same purpose.

Edit: I found a third party C# compiler which does seem to bridge these ideas together.

RemObjects C# also has support for the (rarely needed) manual declaration of Auto-Release Pools via the using (__autoreleasepool) syntax.

http://www.elementscompiler.com/elements/hydrogene/cocoa.aspx

Does using(){...} in C# serve the same purpose as autoreleasepool{...} in Cocoa?


Solution

  • No, they are different.

    C#'s using statement is about resource acquisition and disposal. This is typically an external resource such as a file, where acquisition is opening the file and disposal is closing it.

    Objective-C's auto-release pool is about controlling the lifetime of in memory objects. An object placed in the pool is released when the pool is drained, for the default pool this at end of every iteration of the event loop.