优化foreach的使用

时间:2013-10-22 15:14:21

标签: c# optimization

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

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'在两个分支中执行相同的代码”。

4 个答案:

答案 0 :(得分:5)

只需在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);
    }                     
}

答案 1 :(得分:5)

您可以将代码简化为:

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

答案 2 :(得分:4)

嗯,这不是真正的优化,但你的代码更简单:

if (!Override || match.Groups[2].Value == OverrideFileName)
{
    var df = new DownloadFileStruct(match.Groups[1].Value,
                                    match.Groups[2].Value);
    DownloadFileList.Add(df);
}

(目前还不清楚你在宣布df的位置,但是在<{em>} if语句中声明它是有道理的,假设你实际上没有使用它别的。或者完全摆脱它,只需使用DownloadFileList.Add(new DownloadFileStruct(...))

答案 3 :(得分:0)

试试这个

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

}