Trying to understand the 'using' statement better

北战南征 提交于 2019-12-01 19:02:29

It is perfectly valid to have many nested using statements:

using(A a = new A())
using(B b = new B())
{
   a.SomeMethod(b);
}

You would never be wrong if you use using for every IDisposable that you use. There is no limit of how many nested using blocks you use.

Using statement is syntax sugar of C#.

So the following code:

using(var someDisposableObject = new someDisposableObject())
{
    // Do Something
}

actualy looks like:

var someDisposableObject = new someDisposableObject();
try
{
  // Do Something
}
finally
{
   if (someDisposableObject != null)
   {
       ((IDisposable) someDisposableObject).Dispose();
   }
}

Look at this article: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

There's no limit on the depth, so that's not a concern. You should verify that the object of the using implements IDisposable. And an object being disposed doesn't dispose of all objects connected to it, just those it creates.

So, at what point are you doing wrong: there's no limit, but generally its fairly shallow, you create the object, do a task, then the object is disposed. If you're doing it very deeply, I'd look at the design. I think you'd be hard pressed to do it more than few layers deep.

As for your options for a redesign, that really depends upon what you are doing, but you might use the same object for multiple tasks. Most likely you will end up breaking the task down into a function (passing in any surrounding objects that are needed).

• Is it okay to have 4, 5, 10 nested using statements to ensure all objects are disposed?

Reply: You can not limit of using nested "using blocks ".

• At what point am I doing something wrong and should I consider revision?

Reply: If you have many nested "using blocks". Please try as below.

        using (var con = new SqlConnection(connStr))
        using (var cmd = new SqlCommand(queryStry))
        using (var rs = cmd.ExecuteReader())
        {
            while (rs.Read())
            {
                //Code.
            }
        }

Personally, I have used at least 3 layers (Connection, Command, Other) a number of times and I see absolutely no problem with it, but as you have already hinted at, eventually there will be a problem a readability. As with other nested constructs, you may need to balance efficiency with maintainability. That is, you don't need to necessarily sacrifice efficiency, but there is often 'more than one way to skin a cat'.

That said, you would be hard-pushed to generate 10 nested layers!

IMHO, what you need to ask yourself is: What are the alternatives? Try/finally blocks? Are they more readable? More maintainable? In almost all cases, the answer is going to be "no".

So use using. It's the closest thing C# has to C++'s RAII pattern and it's all good :-)

StuartLC

One time I can think of where you wouldn't want to use 'using' on connections would be on ClassFactories for connected objects such as DataReaders, e.g. consider the case

private IDataReader CreateReader(string queryString,
    string connectionString)
{
    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand command = new SqlCommand(queryString, connection);
    connection.Open();
    return command.ExecuteReader(CommandBehavior.CloseConnection);
    // Don't close connection
}

(Modified from MSDN - The example on MSDN is just plain stupid)

Another reason is on WCF ServiceReference 'clients' - if the channel becomes faulted, 'using' then hides the actual exception. But this is just a buggy implementation IMHO.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!