Multiple variables in a using statement

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

resource-acquisition:

local-variable-declaration

expression

A using statement of the form

using (ResourceType r1 = e1, r2 = e2, …, rN = eN) statement

is precisely equivalent to a sequence of nested using statements:

using (ResourceType r1 = e1)

using (ResourceType r2 = e2)

using (ResourceType rN = eN)

statement

So we could rewrite our Copy method as follows:

     private static void Copy(string source, string destination)
    {
        const int BufferSize = 64 * 1024;
        using (FileStream input = File.OpenRead(source))
        using (FileStream output = File.OpenWrite(destination))
        {
            var buffer = new byte[BufferSize];
            int bytesRead;
            while ((bytesRead = input.Read(buffer, 0, BufferSize)) != 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }
    }

Note however that due to how local-variable-declaration is defined in the language spec, multiple variables have to be of the same type, and you only specify this type once at the beginning of the first declarator.