使用(fs = System.IO.File.OpenWrite(imagePath + imageName)) { fs.Write(myByteArray,0,myByteArray.Length); } } } } catch(例外情况) { } 终于 { if(br!= null) br.Close(); if(fs!= null ) fs.Close(); } 我是否需要使用finally代码或执行使用 ;自动关闭 对象(fs和br)? 此外,当你退出using语句时(有​​或没有错误 - 会将你发送给catch子句,在using语句之外定义的对象是否为null? 谢谢, Tom 解决方案 tshad< ts *** @dslextreme.comwrote: 如果 它是using语句的一部分,我还需要在catch之后关闭finally语句中的对象吗? 否。使用声明的重点是自动完成。 我倾向于只声明每个使用语句中的FileStream fs, 而不是共享一个变量用于两次使用。 (注意FileInfo没有实现IDisposable,所以你的代码 目前实际上并不编译。) 另外,当你退出using语句时(有​​或没有错误) - 会将你发送到catch子句),在using语句之外定义的对象是否为null? 不,它会有相同的价值,只是对象本来是处置的,因此不是非常有用。这是 在using语句本身中声明变量的一个原因 - 它会阻止你尝试使用已经被处置的对象的。 - Jon Skeet - < sk *** @ pobox.com> 网站: http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet C#深度: http://csharpindepth.com " Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言 新闻:MP ********************* @ msnews.microsoft.com 。 .. tshad< ts *** @dslextreme.comwrote: >我是否在捕获之后仍然需要关闭finally语句中的对象如果它是using语句的一部分吗? 否。使用声明的重点是自动完成。 我倾向于只声明每个使用语句中的FileStream fs, 而不是共享一个变量用于两次使用。 (注意FileInfo没有实现IDisposable,所以你的代码 目前实际上并不编译。) >此外,当你退出使用声明时(有或没有)一个错误 - 会将你发送到catch子句),在using语句之外定义的对象是否为null? 不,它会有相同的价值,只是对象本来会被处理掉,因此不是非常有用。这是 在using语句本身中声明变量的一个原因 - 它会阻止你尝试使用已经被处置的对象的。 有道理。 以下内容: using(fs = new FileStream (fromImagePath +" \\" + (string)dr [" originalFileName"],FileMode.Open,System.IO.FileAccess.Read)) { 使用(BinaryReader br = new BinaryReader(fs)) myByteArray = br.ReadBytes((int)numBytes); } 将二进制读取器放在using语句中有点矫枉过正吗? 谢谢, Tom - Jon Skeet - < sk *** @ pobox.com> 网站: http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet C#深度: http://csharpindepth.com tshad< ts *** @ dslextreme.comwrote: 有道理。 以下内容: using(fs = new FileStream(fromImagePath +" \\" + (字符串)dr [" originalFileName"],FileMode.Open,System.IO.FileAccess.Read)) { using(BinaryReader br = new BinaryReader(fs)) myByteArray = br.ReadBytes((int)numBytes); } 将二进制读取器放在using语句中有点矫枉过正吗? 是的,不过我倾向于这样做 - 最好养成 总是处理东西的习惯,并且可以存在这样的包装案例 其中*确实*有所作为(因为关闭内部资源 可以刷新对外部资源的挂起写入)。 /> - Jon Skeet - < sk *** @ pobox.com> 网站: http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet C#深度: http://csharpindepth.com Do I still need to close an object in a finally statement after a catch ifit is part of a using statement?For example:FileStream fs = null;try{using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +(string)dr["originalFileName"])){if (fInfo.Exists){numBytes = fInfo.Length;// Read the file into the byte arrayusing (fs = new FileStream(fromImagePath + "\\" +(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read)){using(BinaryReader br = new BinaryReader(fs))myByteArray = br.ReadBytes((int)numBytes);}// Write out the file into the images folder with new nameusing (fs = System.IO.File.OpenWrite(imagePath + imageName)){fs.Write(myByteArray, 0, myByteArray.Length);}}}}catch (Exception exc){}finally{if (br != null)br.Close();if (fs != null)fs.Close();}Do I need to use the finally code or does the "using" automatically closethe objects (fs and br)?Also, when the you exit the using statement (with or without an error -which would send you to the catch clause), would the an object that wasdefined outside of the using statement be null?Thanks,Tom 解决方案 tshad <ts***@dslextreme.comwrote:Do I still need to close an object in a finally statement after a catch ifit is part of a using statement?No. The whole point of a using statement is to do that automatically.I would tend to only declare the FileStream fs in each using statement,rather than sharing one variable for two uses.(Note that FileInfo doesn''t implement IDisposable, so your codewouldn''t actually compile at the moment.)Also, when the you exit the using statement (with or without an error -which would send you to the catch clause), would the an object that wasdefined outside of the using statement be null?No, it would have the same value, it''s just the object would have beendisposed and therefore not terribly useful. That''s one reason todeclare the variable in the using statement itself - it stops you fromtrying to use an object which has already been disposed.--Jon Skeet - <sk***@pobox.com>Web site: http://www.pobox.com/~skeetBlog: http://www.msmvps.com/jon.skeetC# in Depth: http://csharpindepth.com"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in messagenews:MP*********************@msnews.microsoft.com. ..tshad <ts***@dslextreme.comwrote:>Do I still need to close an object in a finally statement after a catchifit is part of a using statement?No. The whole point of a using statement is to do that automatically.I would tend to only declare the FileStream fs in each using statement,rather than sharing one variable for two uses.(Note that FileInfo doesn''t implement IDisposable, so your codewouldn''t actually compile at the moment.)>Also, when the you exit the using statement (with or without an error -which would send you to the catch clause), would the an object that wasdefined outside of the using statement be null?No, it would have the same value, it''s just the object would have beendisposed and therefore not terribly useful. That''s one reason todeclare the variable in the using statement itself - it stops you fromtrying to use an object which has already been disposed.Makes sense.In the following:using (fs = new FileStream(fromImagePath + "\\" +(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read)){using(BinaryReader br = new BinaryReader(fs))myByteArray = br.ReadBytes((int)numBytes);}Is putting the BinaryReader in the using statement a little overkill?Thanks,Tom--Jon Skeet - <sk***@pobox.com>Web site: http://www.pobox.com/~skeetBlog: http://www.msmvps.com/jon.skeetC# in Depth: http://csharpindepth.com tshad <ts***@dslextreme.comwrote:Makes sense.In the following: using (fs = new FileStream(fromImagePath + "\\" +(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read)) { using(BinaryReader br = new BinaryReader(fs)) myByteArray = br.ReadBytes((int)numBytes); }Is putting the BinaryReader in the using statement a little overkill?Yes, but I tend to do it anyway - it''s better to get into the habit ofalways disposing of things, and there can be wrapping cases like thiswhere it *does* make a difference (because closing the inner resourcemay flush pending writes to the outer resource).--Jon Skeet - <sk***@pobox.com>Web site: http://www.pobox.com/~skeetBlog: http://www.msmvps.com/jon.skeetC# in Depth: http://csharpindepth.com 这篇关于使用,最后声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 17:16