---
title: "Handling IDisposables within another IDisposable “using” statement"  
description: "Handling IDisposables within another IDisposable “using” statement"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1566/handling-idisposables-within-another-idisposable-using-statement  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Handling IDisposables within another IDisposable “using” statement

I'm still relatively new to C# and have only within the past several days been [exposed](https://yourviews.mindstick.com/view/80962/kerala-healthcare-legend-exposed-amidst-coronavirus-pandemic) to "IDisposables". I can grasp the [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) of the using [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) to take care of [objects](https://www.mindstick.com/forum/145447/what-are-objects) which must be disposed of without needing to manually remember to call the .Dispose() [method](https://www.mindstick.com/forum/166/webservice-method) - convenient!

Let's say though that I start with a new [SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) which I [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) within a using statement. Within that block of code I create some additional IDisposables, for example a [SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter). Does that adapter need it's own using statement?

For example, if I have code...

```
using (SqlConnection myConnection = new SqlConnection())
{
    SqlCommand myCommand = new SqlCommand();
    SqlDataAdapter myAdapter = new SqlDataAdapter();
    // Do things
}
```

... will myCommand and myAdapter be disposed of when myConnection is disposed of (since they are within the [scope](https://www.mindstick.com/interview/372/what-are-the-different-scopes-for-java-variables) of that code block)? Or do I need [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) using statements, maybe something like:

```
using (SqlConnection myConnection = new SqlConnection())
{
    using (SqlCommand myCommand = new SqlCommand())
    {
        using (SqlDataAdapter myAdapter = new SqlDataAdapter())
        {
            // Do things
        }
    }
}
```

## Replies

### Reply by Kate Smith

Hello Babe!

Using is just syntatic sugar for

\

\

```
     var connection = new Connection();
        try
        {
            connection.DoSomething();
        }
        finally
        {
            // Check for a null resource.
            if (connection != null)
            {
                ((IDisposable)connection).Dispose();
            }
        }
```

\

So yes, you need nested using statements to be sure to dispose all of these


---

Original Source: https://www.mindstick.com/forum/1566/handling-idisposables-within-another-idisposable-using-statement

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
