Chris
Chris

Reputation: 7611

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 return 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

Upvotes: 0

Views: 1283

Answers (4)

Slappy
Slappy

Reputation: 4092

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).

Upvotes: 1

Lloyd
Lloyd

Reputation: 29668

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

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28834

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500465

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.)

Upvotes: 1

Related Questions