Using Statements

A using statement is used to make sure an object is properly disposed through the IDisposable/Closable interfaces. At the end of the block the object is released.

using fs := new FileStream('textfile.txt', FileMode.Open) do begin
  var b := new byte[123];
  fs.Read(b, 0, b.Length);
end; // the filestream is closed here.


When targetting Cocoa, the using statement has been extended for the Cocoa platform to allow the autoreleasepool keyword to be used in lieu of another expression. This creates a new Auto-Release Pool for this thread and cleans it up at the end of the using statement.

Refer to the Auto-Release Pool topic for more details.

using autoreleasepool do begin
  NSApplicationMain(argc, argv);

Cocoa Only

The using autoreleasepool syntax is relevant and available on the Cocoa platform only.

See Also