在 C# 中返回两个列表的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:12:08  来源:igfitidea点击:

What is the best way to return two lists in C#?

c#listcoding-style

提问by Jeremy White

I am almost embarrassed to ask this question, but as a long time C programmer I feel that perhaps I am not aware of the best way to do this in C#.

我几乎不好意思问这个问题,但作为一个长期的 C 程序员,我觉得也许我不知道在 C# 中做到这一点的最佳方法。

I have a member function that I need to return two lists of a custom type (List<MyType>) and I know beforehand that I will always have a return value of only two of these lists.

我有一个成员函数,我需要返回自定义类型 ( List<MyType>) 的两个列表,并且我事先知道我将始终只有其中两个列表的返回值。

The obvious options are :

显而易见的选择是:

public List<List<MyType>> ReturnTwoLists();

or

或者

public void ReturnTwoLists(ref List<MyType> listOne, ref List<myType> listTwo);

Both seem to be non-optimal.

两者似乎都不是最佳的。

Any suggestions on how to improve this?

关于如何改进这一点的任何建议?

The first way doesn't make it clear in the syntax that only 2 lists are being returned, and the second uses references rather then a return value, which seem so non-c#.

第一种方法在语法中没有明确说明只返回了 2 个列表,第二种方法使用引用而不是返回值,这看起来非常非 c#。

采纳答案by Mehrdad Afshari

First of all, that should probably be out, not ref.

首先,那应该是out,而不是ref

Second, you can declare and return a type containing the two lists.

其次,您可以声明并返回一个包含两个列表的类型。

Third, you can declare a generic Tupleand return an instance of that:

第三,您可以声明一个泛型Tuple并返回它的一个实例:

class Tuple<T,U> {
   public Tuple(T first, U second) { 
       First = first;
       Second = second;
   }
   public T First { get; private set; }
   public U Second { get; private set; }
}

static class Tuple {
   // The following method is declared to take advantage of
   // compiler type inference features and let us not specify
   // the type parameters manually.
   public static Tuple<T,U> Create<T,U>(T first, U second) {
        return new Tuple<T,U>(first, second);
   }
}

return Tuple.Create(firstList, secondList);

You can extend this idea for different number of items.

您可以将这个想法扩展到不同数量的项目。

回答by John Saunders

Return this:

返回这个:

public class MyTwoLists {
    public List<MyType> ListOne {get;set;}
    public List<MyType> ListTwo {get;set;}
}

回答by Ryan O'Neill

Create a simple Structure that holds both and return that as the output of the function?

创建一个包含两者的简单结构并将其作为函数的输出返回?

回答by Joseph

Your first suggestion isn't two lists. It's a list of lists.

您的第一个建议不是两个列表。这是一个列表列表。

The second option would do what you intend, but you might want to change it to use the out keyword instead of ref so the callers of your method will know the intention of what you're doing.

第二个选项将按照您的意图执行,但您可能希望将其更改为使用 out 关键字而不是 ref,以便您的方法的调用者知道您正在执行的操作的意图。

public void ReturnTwoLists(out List<MyType> listOne, out List<myType> listTwo);

回答by JoshJordan

You have a few options:

您有几个选择:

use a Pair if the lists are meaningless in order:

如果列表按顺序没有意义,请使用 Pair :

 public Pair<List<MyType>,List<MyType> ReturnTwoLists()
 {
        return new Pair(new List<MyType(), new List<MyType());
 }

You can use outor refparameters, as you mentioned. This is a good option if one list is more meaningful than the other.

正如您所提到的,您可以使用outref参数。如果一个列表比另一个更有意义,这是一个不错的选择。

You could use a dictionary if the client will know the keys, or wants to do the work to look them up:

如果客户端知道密钥,或者想要查找它们,您可以使用字典:

 public Dictionary<string,List<MyType> ReturnTwoLists()
 {
        Dictionary<string,List<MyTpe>> d = new Dictionary<string,List<MyType>>();
        d.Add("FirstList",new List<MyType>());
        d.Add("SecondList",new List<MyType>());
        return new Dictionary()(new List<MyType(), new List<MyType());
 }

Or, the most "correct" solution in my eyes, for completeness and consistency, would be to create a simple data container class to hold the two lists. This provides a consumer with strongly-typed, good statically compiled (read: intellisense-enabled) return values to work with. The class can be nested right next to the method.

或者,为了完整性和一致性,我眼中最“正确”的解决方案是创建一个简单的数据容器类来保存两个列表。这为消费者提供了强类型、良好的静态编译(阅读:启用智能感知)返回值以供使用。类可以嵌套在方法旁边。