例如
using (Stream ftpStream = ftpResponse.GetResponseStream()) // a
using (FileStream localFileStream = (new FileInfo(localFilePath)).Create()) // b
{
........do something
}
a和b两条语句会按照我放的顺序执行吗? 并以相同的顺序展开??
谢谢
最佳答案
它们将按文本顺序执行,并以相反顺序处理 - 因此 localFileSream
将首先处理,然后是 ftpStream
。
基本上你的代码等同于:
using (Stream ftpStream = ...)
{
using (FileStream localFileStream = ...)
{
// localFileStream will be disposed when leaving this block
}
// ftpStream will be disposed when leaving this block
}
但它远不止于此。您的代码也等效于此(撇开不同类型的 localFileStream
):
using (Stream ftpStream = ..., localFileStream = ...)
{
...
}
关于c# - 当有多个using语句时,它们会按顺序执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4264034/