Returning two lists in C#

后端 未结 6 1482
清歌不尽
清歌不尽 2020-12-06 19:06

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 the

6条回答
  •  孤城傲影
    2020-12-06 19:18

    Method 1

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

    Method 2

    public static Tuple, List> Method2(int[] array, int number)
    {
        list1= new List();
        list2= new List();
        ...
    
        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 list1, List list2) Method2(int[] array, int number)
    {
        ...
        return (list1, list2)
    }
    

提交回复
热议问题