Using Statement in F# September, 2009
I just learned that there is an equivalent in F# to the using statement in C# and VB.NET. There are actually two approaches; one a using function that is passed a function or lambda and the second is a binding similar to the let statement except that it also acts as a using statement when the value goes out of scope. The docs do a good job explaining it. Here is a little demo of the two approaches:
#light open System open System.IO let readFile path = use r = new StreamReader(new FileStream(path, FileMode.Open)) Console.WriteLine(r.ReadToEnd()) let readFile2 path = using (new StreamReader(new FileStream(path, FileMode.Open))) (fun r -> Console.WriteLine(r.ReadToEnd())) readFile @"D:\temp\test.txt" readFile2 @"D:\temp\test.txt" Console.ReadKey() |> ignore