Trying to understand the 'using' statement better

后端 未结 8 1390
小鲜肉
小鲜肉 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:59

    Using statement is syntax sugar of C#.

    So the following code:

    using(var someDisposableObject = new someDisposableObject())
    {
        // Do Something
    }
    

    actualy looks like:

    var someDisposableObject = new someDisposableObject();
    try
    {
      // Do Something
    }
    finally
    {
       if (someDisposableObject != null)
       {
           ((IDisposable) someDisposableObject).Dispose();
       }
    }
    

    Look at this article: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

提交回复
热议问题