Disposables, Using & Try/Catch Blocks

后端 未结 6 1394
一向
一向 2020-12-19 05:56

Having a mental block today, need a hand verifying my logic isn\'t fubar\'ed.

Traditionally I would do file i/o similar to this:

FileStream fs = null         


        
6条回答
  •  既然无缘
    2020-12-19 06:11

    The using block will work exactly as you entend translated the using block is really just

    try
    {
       FileStream fs = null;
       try
       {
            fs = File.Open("Foo.txt", FileMode.Open))
            //Do Stuff
       }
       finally
       {
          if(fs != null)
              fs.Dispose();
       }
    }
    catch(Exception)
    {
       /// Handle Stuff
    }
    

提交回复
热议问题