在C#中嵌套使用语句 [英] Nested using statements in C#
本文介绍了在C#中嵌套使用语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的一个项目,要比较两个文件,并查看它们是否匹配海誓山盟excatly。
I am working on a project and have to compare two files and see if they match eachother excatly.
错误检查和验证之前,很多我的第一份草案提出了:
My first draft before alot of error checking and validation came up with:
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\TestArea\\");
FileInfo[] files = di.GetFiles(filename + ".*");
FileInfo outputFile = files.Where(f => f.Extension == ".out").Single<FileInfo>();
FileInfo expectedFile = files.Where(f => f.Extension == ".exp").Single <FileInfo>();
using (StreamReader outFile = new StreamReader(outputFile.OpenRead()))
{
using (StreamReader expFile = new StreamReader(expectedFile.OpenRead()))
{
while (!(outFile.EndOfStream || expFile.EndOfStream))
{
if (outFile.ReadLine() != expFile.ReadLine())
{
return false;
}
}
return (outFile.EndOfStream && expFile.EndOfStream);
}
}
这似乎有点奇怪using语句嵌套的。
It seems a little odd to have nested using statements.
有没有更好的方式来做到这一点?
Is there a better way to do this?
推荐答案
在preferred办法做到这一点是只把开括号 {
在最后使用
语句,像这样的:
The preferred way to do this is to only put an opening brace {
after the last using
statement, like this:
using (StreamReader outFile = new StreamReader(outputFile.OpenRead()))
using (StreamReader expFile = new StreamReader(expectedFile.OpenRead()))
{
///...
}
这篇关于在C#中嵌套使用语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文