Returning two lists in C #

I have been studying this for a while now and am still not sure how to implement and what is the best way to return two lists from a separate method?

I know there are similar questions around, but they seem to contradict each other, and this is the best way to do this. I just need a simple and effective solution to my problem. Thanks in advance.

+4
source share
6 answers

There are many ways.

  • Gets a collection of lists. This is not a good way to do this if you do not know the number of lists or more than 2-3 lists.

    public static IEnumerable<List<int>> Method2(int[] array, int number)
    {
        return new List<List<int>> { list1, list2 };
    }
    
  • 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 };
    }
    
  • Returning a tuple from two lists is especially convenient if you are 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 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;
    

2 3 . # 7.0 2.

+8

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>();
    ...
}

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

3

, 2 list1, 2,

, , # 7

public static (List<int> list1, List<int> list2) Method2(int[] array, int number)
{
    ...
    return (list1, list2)
}
+1

.NET #, ( 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
}
+1

.

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]);
}
0

, Two Dimensional Array. , "" .

2- , [0,2], :

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

: 21.2

0

/ . :

public static void Method2(int[] arr, List<int> list1, List<int> list2)
{
    list1 = arr.OfType<int>().ToList();
    list2 = arr.OfType<int>().ToList();
}
0

Source: https://habr.com/ru/post/1675042/


All Articles