Using statement to deal with multiple resources : Using Statement « File Directory Stream « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;
using System.IO;             

class MainClass
{
   static void Main()
   {
      using (TextWriter tw1 = File.CreateText("test.txt"),
                        tw2 = File.CreateText("test2.txt"))
      {
         tw1.WriteLine("test");
         tw2.WriteLine("test1");
      }

      using (TextReader tr1 = File.OpenText("test.txt"),
                        tr2 = File.OpenText("test2.txt"))
      {
         string str;
         while (null != (str = tr1.ReadLine()))
            Console.WriteLine(str);
            
         while (null != (str = tr2.ReadLine()))
            Console.WriteLine(str);
      }
   }
}
test
test1








15.33.Using Statement
15.33.1.Use object inside using statement
15.33.2.Create object inside the using statement
15.33.3.Using Statement: using DIRECTIVE, not using statement
15.33.4.Use FileStream inside a using statement
15.33.5.Dispose a StreamWriter by using 'using'
15.33.6.Using statement to deal with multiple resources
15.33.7.Nested using statement