How can I return different lists of objects at one method? I have some methods, that returns some types of Lists, and I don't know what Parse method should return
public static [Something here (IList???)] Parse(string filePath)
{
string[] lines = File.ReadAllLines(filePath);
for (int i = 0; i < lines.Length; i++)
{
string recordId = lines[i].Substring(0, 2);
switch (recordId)
{
case "00":
return Parse00(lines[i]); //public static List<mainInfo> Parse00(string line);
case "11":
return Parse11(lines[i]); //public static List<param> Parse11(string line);
…
…
}
}
}
mainInfoinherit fromparam, or vice versa, or do they inherit from some common type?List<object>orList<dynamic>then (or any other collection type), that's about it. Your code isn't altogether clear, however. The first line that matches a case in your switch statement will return something and abort the loop, is this code really representative for what you want to accomplish?List<MyDerivedType>cannot implicitly be cast to aList<MyBaseType>. See this SO answer for clarification. I know this is not something you've explicitly stated in your comment, but I infer that that's where you were taking this train of thought to (as would other readers, I assume)