Skip to content

Basic 'using' keyword Usage

CodingUnit edited this page Dec 1, 2011 · 1 revision

Basic Using Usage

  • Category: Disposal
  • Description: The 'using' keyword 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.
  • Code:
using Nemerle;
using System;
using System.Console;
using System.IO;

module Test
{
  Main() : void
  {
    File.WriteAllLines(@"test.txt", array["This is a test file.", "It is easy to read."] );

    using (sr = File.OpenText("test.txt"))
    {
      def line1 = sr.ReadLine();
      def line2 = sr.ReadLine();
      WriteLine($"line1 = $line1");
      WriteLine($"line2 = $line2");
    }
  }
}
  • Execution Result:
line1 = This is a test file.
line2 = It is easy to read.
Clone this wiki locally