开发者

Returning multiple lists in C#

I'm using a custom class to store information about files found in a directory based upon a regex, and I need to ret开发者_Python百科urn these filelists from a method. Here's some psuedocode of how it's currently working:

class FileList {
//propertes of the file here, such as regex name, source folder and whether it needs to be deleted after copying
}

..
..

foreach (file in directory that matches regex)
{
    new filelist
    //Set all the properties
}

return allfilelists;

But how do I return the all the lists, or is there a better way of doing this?

Thanks


Why not return a list of FileLists?

var allfilelists = new List<FileList >();
foreach (file in directory that matches regex)
{
    new filelist
    //Set all the properties
    allfilelists.Add(fileList);
}


Any reason not to return a List<FileList> or something similar? (There's a choice of abstraction here - you could declare that the method returns IEnumerable<FileList>, IList<FileList>, List<FileList> etc.)


Alternatively you can return IEnumerable<FileList> on the method and use the yield keyword to use deferred execution and eliminate the overhead of the List object (as little as it is).


You can use List<FileList> or FileList[].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜