Is it correct to use a block using inside another using block?

Asked

Viewed 314 times

18

It is correct to use a block using inside another block using as in the example below? or just put the first using?

    public DataTable Listar(string stringMySql, CommandType commandType, List<MySqlParameter> lstParametros)
    {
        using (MySqlConnection mySqlConnection = new MySqlConnection())
        {
            mySqlConnection.ConnectionString = StaticKey.RetornaStringConexao();
            mySqlConnection.Open();

            using (MySqlCommand mySqlCommand = new MySqlCommand())
            {
                mySqlCommand.Connection = mySqlConnection;
                mySqlCommand.CommandType = commandType;
                mySqlCommand.CommandText = stringMySql.Trim();

                if (lstParametros != null)
                {
                    mySqlCommand.Parameters.AddRange(lstParametros.ToArray());
                }

                using (DataTable dataTable = new DataTable())
                {
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(mySqlCommand))
                    {
                        mySqlDataAdapter.Fill(dataTable);

                        return dataTable;
                    }
                }
            }
        }
    }

1 answer

16


It is absolutely correct. Every resource needs its own using to ensure its closure when no longer needed. You can improve a little here:

using (DataTable dataTable = new DataTable())
using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(mySqlCommand)) {
    mySqlDataAdapter.Fill(dataTable);
    return dataTable;
}

It is possible to use a single block with more than one resource open. At the end of the block, both will be closed.

There’s actually a problem there. The code is returning something that is being willing. This is not going to work. If you will return the resource out of the method, you cannot dispose of it. In this case the using should not be used for the resource to remain alive. Thus:

var dataTable = new DataTable();
using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(mySqlCommand)) {
    mySqlDataAdapter.Fill(dataTable);
    return dataTable;
}

Or in some cases (not in):

var dataTable = new DataTable();
try {
    using (var mySqlDataAdapter = new MySqlDataAdapter(mySqlCommand)) {
        mySqlDataAdapter.Fill(dataTable);
        return dataTable;
    }
} finally {
    if (dataTable != null) dataTable.Dispose();
}

I put in the Github for future reference.

But it is necessary that whoever calls this your method do the release of the resource manually or use a using. This is the tip ode code that I see a lot of people make mistakes and then don’t understand why problem even using the using.

An interesting detail is that now (C# 8) no longer need to create blocks for the using, only declare the variable without the block and the scope becomes the current block. Only this:

var dataTable = new DataTable();
using var mySqlDataAdapter = new MySqlDataAdapter(mySqlCommand));
mySqlDataAdapter.Fill(dataTable);
return dataTable;

In C# 8 you can already use using var so you don’t have to create a block just for the using.

  • 1

    Thank you for the answer, programming in a certain way is easy the difficult for me often is to be sure that what I am doing is being done in the correct and clean way and in this case your answer left well clarified not to err more.

  • That’s why we’re here.

  • @Maniero, enlightening and objective answer. Very good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.