.NET - Replacing nested using statements with single using statement

后端 未结 5 841
说谎
说谎 2021-01-12 19:08

If you came across some C# code like this with nested using statements/resources:

using (var response = (HttpWebResponse)request.GetResponse())
{
    using (         


        
5条回答
  •  旧巷少年郎
    2021-01-12 19:40

    You should just stack your using statements - it has the desired effect you are looking for:

    using (var response = (HttpWebResponse)request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var reader = new BinaryReader(responseStream))
    {
        // do something with reader
    }
    

提交回复
热议问题