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
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.)
或者,您可以在该方法上返回IEnumerable<FileList>
,并使用yield
关键字来使用延迟执行并消除List对象的开销(尽可能少)。
您可以使用List<FileList>
或FileList[]
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.