2

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?

Community
  • 1
  • 1
Chéyo
  • 9,107
  • 9
  • 27
  • 44
  • That link says this: RemObjects C# also has support for the (rarely needed) manual declaration of Auto-Release Pools via the using (__autoreleasepool) syntax. – CodingYoshi Dec 02 '16 at 22:35

1 Answers1

3

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.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • So while the autoreleasepool can also release resources as a side effect of their objects going out of scope; C#'s using() specific use is to dispose of resources (closing connections etc) but with a side effect of letting know GC that the objects memory can be reclaimed? – Chéyo Dec 03 '16 at 00:58
  • 1
    No. C# `using` is not directly associated with GC at all. Resource acquisition *may* involve object creation, it need not. Resource disposal calls `Dispose()`, it does not inform GC the object is no longer required, and the object *may* continue to be used. An object implementing `Disposable` is *not* required to call its own `Dispose()` as part of its finaliser. While typical uses of `using` may involve allocation and deallocation of the resource object, but that is a usage pattern and *not* a requirement. – CRD Dec 03 '16 at 05:05