This is a mirror of official site: http://jasper-net.blogspot.com/

Multiple variables in a using statement

| Tuesday, October 12, 2010
Here’s a simple file copy program:

using System;
using System.IO;

public class FileCopy
{
   private static void Main(string[] args)
   {
       if (args.Length != 2)
       {
           Console.WriteLine("Usage: filecopy <source> <destination>");
           return;
       }

       Copy(args[0], args[1]);
   }

   private static void Copy(string source, string destination)
   {
       const int BufferSize = 64 * 1024;
       using (FileStream input = File.OpenRead(source), output = File.OpenWrite(destination))
       {
           var buffer = new byte[BufferSize];
           int bytesRead;
           while ((bytesRead = input.Read(buffer, 0, BufferSize)) != 0)
           {
               output.Write(buffer, 0, bytesRead);
           }
       }
   }
}
I’ve only recently learned that you can use multiple local variable declarators in the using statement, separated by comma, as long as they are of the same type. In the spec, section 8.13 (The using Statement), it says:

using-statement:

using ( resource-acquisition ) embedded-statement

Read more: Kirill Osenkov

Posted via email from .NET Info

0 comments: