当有多个using语句时,它们会按顺序执行吗?
例如,
using (Stream ftpStream = ftpResponse.GetResponseStream()) // a
using (FileStream localFileStream = (new FileInfo(localFilePath)).Create()) // b
{
........do something
}
两个语句 a 和 b 会按照我输入的顺序执行吗? 并以相同的顺序丢弃?
谢谢
for example
using (Stream ftpStream = ftpResponse.GetResponseStream()) // a
using (FileStream localFileStream = (new FileInfo(localFilePath)).Create()) // b
{
........do something
}
will the two statement a and b executed in the order i put?
and displose in the same order as well??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们将以文本顺序执行,并以相反的顺序进行处理 - 因此,
localFileSream
将首先被处理,然后是ftpStream
。基本上你的代码相当于:
但它比这更进一步。您的代码也与此等效(不考虑不同类型的
localFileStream
):They will execute in textual order, and be disposed in reverse order - so
localFileSream
will be disposed first, thenftpStream
.Basically your code is equivalent to:
It goes further than that though. Your code is also equivalent (leaving aside the different type of
localFileStream
) to this:是的。此语法只是嵌套
using
语句的快捷方式或替代方式。您所做的只是省略第一个using
语句中的括号。它相当于:Yes. This syntax is just a shortcut or alternative way of nesting
using
statements. All you're doing is omitting the brackets on the firstusing
statement. It's equivalent to: