我一直认为在using
之前声明变量可以允许在using
中对其进行赋值,然后我仍然可以在外部读取该变量。结果发现我不行 :-)
ReadOnlyCollection<string> collection;
using (var archive = new SevenZipArchive(varRarFileName)) {
collection = archive.Volumes;
MessageBox.Show(collection.Count.ToString()); // Output 10
}
MessageBox.Show(collection.Count.ToString()); // output 0
有没有办法在不停止使用using
的情况下使其正常工作?
完整的测试方法:
private ReadOnlyCollection<string> ExtractRar(string varRarFileName, string varDestinationDirectory) {
ReadOnlyCollection<string> collection;
using (var archive = new SevenZipArchive(varRarFileName)) {
collection = new ReadOnlyCollection<string>(archive.Volumes);
MessageBox.Show(collection.Count.ToString()); // output 10
}
MessageBox.Show(collection.Count.ToString()); // output 0
return collection;
}