Basic Use Bindings

Description: The 'use' binding indicates that the IDisposable.Dispose method should be called on the object at the end of its lexical scope. In this case it closes the file deterministically.
let DisposeSample1() =
System.IO.File.WriteAllLines(@"test.txt", [| "This is a test file.";
"It is easy to read." |]);

use sr = System.IO.File.OpenText @"test.txt"
let line1 = sr.ReadLine()
let line2 = sr.ReadLine()
printfn "line1 = %s" line1
printfn "line2 = %s" line2

Execution Result:
line1 = This is a test file.
line2 = It is easy to read.

Last edited Sep 14, 2011 at 2:40 AM by ttliu2000, version 1


No comments yet.