Hirdetés

Új hozzászólás Aktív témák

  • Froclee

    őstag

    válasz vlevi #7276 üzenetére

    Deferred execution egyébként érdekes téma. Akit érdekel, debugger-rel biggyesszen egy breakpoint-ot a foreach-re és lassan nyomogassa az F10-et, és nézze a konzol output:

    string sourceFolderPath = $"C:\\Users\\{Environment.UserName}\\Desktop";
    string filePattern1 = @".txt";
    string filePattern2 = @".exe";
    var matchingFiles = Directory.GetFiles(sourceFolderPath, "*", SearchOption.TopDirectoryOnly).Where(x =>{
    Console.WriteLine($"executing now on {x}");
    return x.EndsWith(filePattern1) || x.EndsWith(filePattern2);
    });
    foreach (string filePath in matchingFiles)
    {
    Console.WriteLine("one iteration in foreach");
    Console.WriteLine(filePath);
    }

    És nézze meg ugyanezt, ha .ToList()-et végére biggyeszt.

    var matchingFiles = Directory.GetFiles(sourceFolderPath, "*", SearchOption.TopDirectoryOnly).Where(x =>{
    Console.WriteLine($"executing now on {x}");
    return x.EndsWith(filePattern1) || x.EndsWith(filePattern2);
    }).ToList();

Új hozzászólás Aktív témák