I have a custom list which inherits from Generic.List<T> like this:
public class TransferFileList<T> : List<TransferFile> { .. }
When I set (where ‘Files‘ is a TransferFileList<T>):
var files = uploadResponse.Files.Where(x => !x.Success).ToList()
the ‘files‘ object resolves as System.Collections.Generic.List<TransferFile>, not TransferFileList<T>, which is what I would expect as it was what was being filtered through the Where, so how could I successfully return a list of TransferFileList<T> into ‘files’?
I did try:
var files = uploadResponse.Files.Where(x => !x.Success).ToList()
as TransferFileList<TransferFile>;
but using that safe cast, it just resolves as null.
Thanks guys and gals.
First, I have to ask why you are inheriting from
List<T>? 99% of the time that’s a bad idea.If you want to extend the functionality of a list, use extension methods:
On to the answer:
ToList()operates on anIEnumerable<T>and converts the members of the sequence to aListof the same type. Since you inherit fromList<T>which implementsIEnumerable<T>, that’s what happens there.Where()works the same way – operates on anIEnumerable<T>and returns anIEnumerable<T>.To get some arbitrary list-like object back, like you have, you need to add the items in a sequence to your custom list, like so: