Trying to understand the 'using' statement better

后端 未结 8 1398
小鲜肉
小鲜肉 2021-01-18 17:26

I have read a couple of articles about the using statement to try and understand when it should be used. It sound like most people reckon it should be used as much as possib

8条回答
  •  忘掉有多难
    2021-01-18 17:58

    • 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.
                }
            }
    

提交回复
热议问题