User Tools

Site Tools


using

The using statement is very useful if you are working with objects that need to be disposed by the garbage collector at a defined point.

using System.IO
using System.Text

function Start( ) as void                     
local cFileName as string
local aBytes as byte[]
	                         
cFileName := "c:\temp\Using.txt"
System.Console.WriteLine("start working with" + cFileName )
begin using var oFile := FileStream{ cFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite } // as FileStream
  aBytes := Encoding.ASCII.GetBytes( "Hello world!" )
  oFile:Write( aBytes, 0, aBytes:Length )
end using                                                
// file is not only out of scope, but also disposed, i.e. closed
System.Console.WriteLine("end working with" + cFileName )
System.Console.Write( "press a key...." )
System.Console.ReadKey()
	
return
using.txt · Last modified: 2018/04/22 13:10 by wolfgangriedmann