对于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;
}
posted @ 2011-11-12 20:52  陈跳跳  阅读(564)  评论(1编辑  收藏  举报