优化使用foreach

本文关键字:foreach 优化 | 更新日期: 2023-09-27 18:36:28

我有一小段代码,其中每个代码都被使用。我已经优化了我的代码,但我的老板希望我进一步优化它。我不知道这里可以做什么进一步的优化。

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'"。

优化使用foreach

您可以将代码简化为:

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();

只需在 if 中使用 OR 即可,而不是重复两次代码。

它与优化无关,而是与不必维护 2 个执行完全相同操作的代码分支有关。

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);
    }                     
}

好吧,这不是真正的优化,但您的代码更简单:

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(new 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);
    }
}