10

I have been researching this for a while now, and am still unsure on how to implement and what is the best way to return two lists from a separate method?

I know there are similar question floating around but they seem to contradict each other as to which is the best way to do this. I just need simple and effective resolution to my problem. Thanks in advance.

L. Full
  • 440
  • 2
  • 5
  • 17

6 Answers6

35

There are many ways.

  1. Return a collection of the lists. This isn't a nice way of doing it unless you don't know the amount of lists or if it is more than 2-3 lists.

    public static IEnumerable<List<int>> Method2(int[] array, int number)
    {
        return new List<List<int>> { list1, list2 };
    }
    
  2. Create an object with properties for the list and return it:

    public class YourType
    {
        public List<int> Prop1 { get; set; }
        public List<int> Prop2 { get; set; }
    }
    
    public static YourType Method2(int[] array, int number)
    {
        return new YourType { Prop1 = list1, Prop2 = list2 };
    }
    
  3. Return a tuple of two lists - Especially convenient if working with C# 7.0 tuples

    public static (List<int>list1, List<int> list2) Method2(int[] array, int number) 
    {
        return (new List<int>(), new List<int>());
    }
    
    var (l1, l2) = Method2(arr,num);
    

    Tuples prior to C# 7.0:

    public static Tuple<List<int>, List<int>> Method2(int[] array, int number)
    {
        return Tuple.Create(list1, list2); 
    }
    //usage
    var tuple = Method2(arr,num);
    var firstList = tuple.Item1;
    var secondList = tuple.Item2;
    

I'd go for options 2 or 3 depending on the coding style and where this code fits in the bigger scope. Before C# 7.0 I'd probably recommend on option 2.

Graham
  • 7,431
  • 18
  • 59
  • 84
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • 1
    Thanks for not suggesting using `ref` and `out`. Using those will make the signature more prone to change where the other solutions do not. – Mixxiphoid Apr 18 '17 at 11:40
  • Using the C# 7.0 tuples reduces excess code while maintaining the simplicity of having return values left of the method name and input parameters on the right in parentheses. – Cale Sweeney Oct 28 '22 at 15:57
3

Method 1

public static void Method2(int[] array, out List<int> list1, out List<int> list2, int number)
{
    list1= new List<int>();
    list2= new List<int>();
    ...
}

Method 2

public static Tuple<List<int>, List<int>> Method2(int[] array, int number)
{
    list1= new List<int>();
    list2= new List<int>();
    ...

    return Tuple.Create(list1, list2)
}

Method 3

Create a class that have 2 props list1, list 2, return that class, or just return array of lists

and finally on C# 7 you can just do

public static (List<int> list1, List<int> list2) Method2(int[] array, int number)
{
    ...
    return (list1, list2)
}
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
3

If you are using later version of .NET and C# then simply use tuples (you may need to Install-Package "System.ValueTuple")

public static void Method1()
{
    int[] array1 = { };
    int number1 = 1;
    (List<int> listA, List<int> listB) = Method2(array1, number1);
}

public static (List<int>, List<int>) Method2(int[] array, int number)
{
    List<int> list1 = new List<int>();
    List<int> list2 = new List<int>();

    return (list1, list2); //<--This is where i need to return the second list
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Digvijay
  • 774
  • 5
  • 10
1

You should pass both the desired lists as a reference to the calling function. For example

public static void Method1()
{
    List<int> listA, listB;
    Method2(array1, number1, ref listA, ref listB);
}

public static void Method2(int[] array, int number, ref List<int> listA, ref List<int> listB)
{
    //...do stuff here
    listA.Add(array[value]);
    listB.Add(array[value]);
}
1

You could look at structuring your return into a Two Dimensional Array. This is essentially a list of lists and can be visualised as a graph where each 'coordinate' contains a value.

Here is an example of creating a 2-Dimensional array, adding a value to point [0,2] and then getting its value from that point and writing it to the screen:

double[,] myNumbers = new double[4, 3];
myNumbers[0, 2] = 21.2;
Console.WriteLine(myNumbers[0,2]);

Output: 21.2

1

A better practice IMO would be passing two lists to your desired method and initializing / assigning them from within the method itself. Example:

public static void Method2(int[] arr, List<int> list1, List<int> list2)
{
    list1 = arr.OfType<int>().ToList();
    list2 = arr.OfType<int>().ToList();
}
Tommy Naidich
  • 752
  • 1
  • 5
  • 23
  • Please read Jon's answer [here](http://stackoverflow.com/a/635934/6400526). Also this is already covered in Arsen's answer – Gilad Green Apr 18 '17 at 12:05