C# 优化foreach的使用
我有一小部分代码,其中每个都使用for。我已经优化了我的代码,但我的老板希望我进一步优化它。我不知道这里可以做什么进一步的优化C# 优化foreach的使用,c#,optimization,C#,Optimization,我有一小部分代码,其中每个都使用for。我已经优化了我的代码,但我的老板希望我进一步优化它。我不知道这里可以做什么进一步的优化 foreach (Match match in matches) { //When oveeride is false and you have to download all the files if (Override.Equals(false)) { //putting the matches from regular expres
foreach (Match match in matches) {
//When oveeride is false and you have to download all the files
if (Override.Equals(false)) {
//putting the matches from regular expression into DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
//When override is true and a paticular file has to be downloaded
else if (match.Groups[2].Value.Equals(OverrideFileName)) {
//putting the matche from regular expression into a DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
}
}
正如我的老板所说,“您不需要在两个分支中执行相同代码的‘if’和‘else if’” 只需在if中使用OR,而不是重复代码两次 这不是关于优化,而是关于不必维护两个做完全相同事情的代码分支
foreach (Match match in matches) {
//When oveeride is false and you have to download all the files
if (Override.Equals(false) || match.Groups[2].Value.Equals(OverrideFileName)) {
//putting the matches from regular expression into DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
}
您可以将代码简化为:
foreach (Match match in matches)
{
if (Override && !match.Groups[2].Value.Equals(OverrideFileName))
continue;
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
DownloadFileList.Add(df);
}
或者LINQ-ish:
DownloadFileList =
matches.Cast<Match>()
.Where(x => !Override || x.Groups[2].Value.Equals(OverrideFileName))
.Select(x => new DownloadFileStruct(x.Groups[1].Value,
x.Groups[2].Value))
.ToList();
下载文件列表=
matches.Cast()
.Where(x=>!Override | | x.Groups[2]。Value.Equals(OverrideFileName))
.Select(x=>newdownloadfilestruct(x.Groups[1])。值,
x、 组[2]。值)
.ToList();
这不是真正的优化,但您的代码会更简单,因为:
if (!Override || match.Groups[2].Value == OverrideFileName)
{
var df = new DownloadFileStruct(match.Groups[1].Value,
match.Groups[2].Value);
DownloadFileList.Add(df);
}
(不清楚您在哪里声明了df
,但是在if
语句中声明它是有意义的,假设您实际上没有在其他地方使用它。或者完全去掉它,只使用DownloadFileList.Add(新的DownloadFileStruct(…)
试试这个。)
foreach (Match match in matches) {
//When oveeride is false and you have to download all the files
//OR
//When override is true and a paticular file has to be downloaded
if ((Override.Equals(false)) || (match.Groups[2].Value.Equals(OverrideFileName))) {
//putting the matches from regular expression into DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
}
这可能是开着的。你真的告诉我们你可以匹配正则表达式,但不能将两个逻辑语句组合成一个吗?他不是在要求我们通常所指的优化(性能改进),但似乎是建议改进代码的组织方式,使其更易于维护。@nvoigt:确切地说,我不是指从性能角度来看的代码优化,而是从t=可维护性角度来看的代码优化。实际上,此df用于不同的功能中。因此,它必须there@user2754845当前位置正在用于r所有地方都是一样的?你真的希望值是“添加到列表中的最后一项”吗?这听起来很奇怪-一般来说,最好尽可能严格地限制每个局部变量的范围。