User Tools

Site Tools


using

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

using [2018/04/22 13:10] (current)
wolfgangriedmann created
Line 1: Line 1:
 +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.
 +<code>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</code>
  
using.txt ยท Last modified: 2018/04/22 13:10 by wolfgangriedmann