对于using嵌套的改进

public IEnumerable<Order> GetOrders()

{

    var orders = new List<Order>();

    using (var con = new SqlConnection("some connection string"))

    {

        using (var cmd = new SqlCommand("select * from orders", con))

        {

            using (var rs = cmd.ExecuteReader())

            {

                while (rs.Read())

                {

                    // ...

                }

            }

        }

    }

    return orders;

}
出现using嵌套,影响代码的美观
对其改进:
public IEnumerable<Order> GetOrders()

{

    var orders = new List<Order>();



    using (var con = new SqlConnection("some connection string"))

    using (var cmd = new SqlCommand("select * from orders", con))

    using (var rs = cmd.ExecuteReader())

    {

        while (rs.Read())

        {

            // ...

        }

    }

    return orders;

}

你可能感兴趣的:(in)