AlIon
AlIon

Reputation: 367

Return different Lists at one method C#

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

          …  
          …

        }
    }
}

Upvotes: 0

Views: 1458

Answers (4)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

You need your mainInfo and param-classes to inherit the same base-class or interface:

interface MyInterface { ... }
class Param : MyInterface { ... }
class MainInfo : MyInterface { ... }

Now you can return lists of the desired types:

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

However your methods need to return Enumerable<MainInfo> or IEnumerable<Param> as List isn´t covariant which means you can´t assign a List<MainInfo> to List<MyInterface>, however you can assign the former to IEnumerable<MyInterface> as IEnumerable is covariant.

Finally as both possible return-values are assignable to IEnumerable<MyInterface> you could use that as return-type for your Parse-method.

Upvotes: 0

BOR4
BOR4

Reputation: 630

I would just like to point out to you that you would save some memory if you read lines like this.

var lines = File.ReadLines(fileName); foreach (var line in lines) // Process line

You can learn more about this on What's the fastest way to read a text file line-by-line?.

Furthermore, I would rename your method to ParseFile to avoid confusion and use @CodeCaster approach with creating class hierarchy. Once you have class hierarchy you can write a Factory that would do the parsing for you.

Upvotes: 1

tech2avinash
tech2avinash

Reputation: 73

Use List<dynamic> as a return type. Ex:

    public static List<dynamic> 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]).ToList<dynamic>; //public static List<mainInfo> Parse00(string line);

            case "11":

                return Parse11(lines[i]).ToList<dynamic>; //public static List<param> Parse11(string line);

          …  
          …

        }
    }
}

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151584

In order for this to work, you need a base type that all "records" inherit from. So call it Record:

public class Record
{
    // some shared properties
}

public class ParamRecord : Record
{
    // param-specific properties
}   

public class MainInfoRecord : Record
{
    // main-specific properties
}   

Now you can return a List<Record>.

Upvotes: 1

Related Questions