Stan
Stan

Reputation: 26511

Is combining `using` statements same as doing them one-by-one?

Note: I'm sorry for the weird title but I cannot figure out any better title, feel free to edit.

So I have very simple question regarding using block in C#. I know what it does and why to use it on IO and network operations however I need to know if grouping them is OK since if I have multiple of them it's too much identation.

So basically the question is simple: Are these two functions underneath are the same? Or I should use the latter one with the multi-level indentation?

public static async Task ReadStuffUsingTogether()
{
    using (var client = new HttpClient())
    using (var response = await client.GetAsync("http://bigfile.com/file.csv", HttpCompletionOption.ResponseHeadersRead))
    using (var fileStream = await response.Content.ReadAsStreamAsync())
    using (var writeStream = File.Open("1.csv", FileMode.Create))
    {
        await fileStream.CopyToAsync(writeStream);
    }
}

// Too much identation in my opinion
public static async Task ReadStuffUsing()
{
    using (var client = new HttpClient())
    {
        using (var response = await client.GetAsync("http://bigfile.com/file.csv", HttpCompletionOption.ResponseHeadersRead))
        {
            using (var fileStream = await response.Content.ReadAsStreamAsync())
            {
                using (var writeStream = File.Open("1.csv", FileMode.Create))
                {
                    await fileStream.CopyToAsync(writeStream);
                }
            }
        }
    }
}

Upvotes: 2

Views: 82

Answers (3)

Fabjan
Fabjan

Reputation: 13676

Uhm, well it is the same. The rules here are the same as for nested ‘if’ statements :

if(x) 
if(y) 
if(z) DoSomething();

Or:

if(x)
{
   if(y) 
   {
       if(z)
       {
           DoSomething(); 
           .. some other code
       }
   }
}

The main difference between the two is that with curly brackets it is possible to add a block of code whilst without - only one line

Upvotes: 7

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

Yes, combining them in your case is exactly the same as doing them one-by-one.

The syntax of using opens a single-statement scope, in the same way as a for loop or an if statement does. You formatted them one on top of the other; indenting would change nothing, because C# ignores whitespace:

using (var client = new HttpClient())
    using (var response = await client.GetAsync("http://bigfile.com/file.csv", HttpCompletionOption.ResponseHeadersRead))
        using (var fileStream = await response.Content.ReadAsStreamAsync())
            using (var writeStream = File.Open("1.csv", FileMode.Create))
            {
                await fileStream.CopyToAsync(writeStream);
            }

It limits your usage slightly, because you cannot add more code after one using ends but before the next one ends:

using (var fileStream = await response.Content.ReadAsStreamAsync())
{
    using (var writeStream = File.Open("1.csv", FileMode.Create))
    {
        await fileStream.CopyToAsync(writeStream);
    }
    // Do something else with fileStream
    log.Info("Read {0} bytes from fileStream", fileStream.Position);
}

However, if this is not necessary, you can skip nesting of curly braces.

Upvotes: 4

Jamiec
Jamiec

Reputation: 136124

They are exactly the same, you're taking advantage of the fact that any block with only one statement can omit the { and }

its essentailly the same as doing this

if(someCondition)
    Console.WriteLine("foo");

Upvotes: 2

Related Questions