本文介绍了匿名实例和处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用(BinaryReader reader = new BinaryReader(new FileStream(filePathName, FileMode.Open))) { // do这里的东西 } 我在其中一个论坛中看到过这段代码。在这个文件中,当BinaryReader处理时,提供的流实例会被处理掉吗? 谢谢 using(BinaryReader reader = new BinaryReader(new FileStream(filePathName,FileMode.Open))){//do something here}I have seen this code in one of the discussion forum. In this will filestream instance supplied get disposed when BinaryReader disposes ? Thanks推荐答案 是的,当BinaryReader上调用Dispose 时,BinaryReader将处理其底层流。 Jon Yes, BinaryReader will dispose of its underlying stream when Disposeis called on the BinaryReader. Jon 应该,是的。 It should, yes. 它应该,是的。 It should, yes. 我在这种情况下遇到了问题 - 不知道为什么我有奇怪的行为。但取出FileStream 实例化解决了这个问题。基本上代码看起来像: 使用(FileStream fs = new new FileStream(filePathName, FileMode.Open)) 使用(BinaryReader reader = new BinaryReader(fs) { // TODO: } I''ve experienced problem with this kind of instansiation - don''t knowwhy I got strange behavior. But taking out the FileStreaminstantiation solved the problem. Basically the code will look like: using (FileStream fs = new new FileStream(filePathName,FileMode.Open))using (BinaryReader reader = new BinaryReader(fs){//TODO:} 这篇关于匿名实例和处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 19:42