【问题标题】:Returning two lists in C#在 C# 中返回两个列表
【发布时间】:2017-09-14 04:57:34
【问题描述】:

我已经对此进行了一段时间的研究,但仍然不确定如何实现以及从单独的方法返回两个列表的最佳方法是什么?

我知道周围有类似的问题,但它们似乎相互矛盾,什么是最好的方法。 我只需要简单有效地解决我的问题。 提前致谢。

【问题讨论】:

  • 返回列表>
  • stackoverflow.com/a/635934/552420 那里接受的答案不鼓励在 C# 中使用 ref 和 out 因为它不应该是必需的,除了 Try 方法。

标签: c# return


【解决方案1】:

有很多方法。

  1. 返回列表的集合。除非您不知道列表的数量或列表超过 2-3 个,否则这不是一个好方法。

    public static IEnumerable<List<int>> Method2(int[] array, int number)
    {
        return new List<List<int>> { list1, list2 };
    }
    
  2. 创建一个具有列表属性的对象并返回它:

    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. 返回一个包含两个列表的元组 - 使用时特别方便 C# 7.0 元组

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

    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,具体取决于编码风格以及此代码适合更大范围的位置。在 C# 7.0 之前,我可能会推荐选项 2。

【讨论】:

  • 感谢您不建议使用refout。使用这些将使签名更容易在其他解决方案没有的地方发生变化。
【解决方案2】:

方法一

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

方法二

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

方法三

创建一个有 2 个道具 list1、list 2 的类,返回该类,或者只返回列表数组

最后在 C# 7 上你可以做到

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

【讨论】:

  • 返回 void 并使用 out 似乎很奇怪。我一直认为“out”应该用于 Try 方法。
  • 你可以在这里使用,它会起作用,它是否是正确的设计解决方案是另一个问题
  • 我确实在质疑设计。我很确定它会起作用。
【解决方案3】:

如果您使用的是更高版本的 .NET 和 C#,则只需使用元组(您可能需要 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
}

【讨论】:

    【解决方案4】:

    您可以考虑将您的回报结构化为Two Dimensional Array。 这本质上是一个列表列表,可以可视化为一个图表,其中每个“坐标”都包含一个值。

    这是一个创建二维数组的示例,将值添加到点 [0,2],然后从该点获取其值并将其写入屏幕:

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

    输出:21.2

    【讨论】:

      【解决方案5】:

      您应该将所需的两个列表作为对调用函数的引用传递。 例如

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

      【讨论】:

      • List 不是已经是参考了吗?为什么在这里使用 ref 关键字?
      • 是的,如果您不想添加“ref”关键字,它已经是一个参考,但它会很好,并且分配将存储在列表中
      • 那为什么还要加上 ref 关键字呢?
      • 只是为了理解。
      【解决方案6】:

      IMO 更好的做法是将两个列表传递给您想要的方法,并从方法本身中初始化/分配它们。 示例:

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

      【讨论】:

      • 请阅读 Jon 的回答 here。这也已经包含在 Arsen 的回答中
      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      相关资源
      最近更新 更多