Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C#返回两个列表的最佳方法是什么?_C#_List_Coding Style - Fatal编程技术网

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

用C#返回两个列表的最佳方法是什么?,c#,list,coding-style,C#,List,Coding Style,我几乎不好意思问这个问题,但作为一个长期的C程序员,我觉得也许我不知道用C做这件事的最佳方法 我有一个成员函数,需要返回两个自定义类型的列表(List),而且我事先知道,我的返回值总是仅为这些列表中的两个 显而易见的选择是: public List<List<MyType>> ReturnTwoLists(); public List returnTwoList(); 或 public void returnTwoList(参考列表列表一,参考列表二); 两者似乎都

我几乎不好意思问这个问题,但作为一个长期的C程序员,我觉得也许我不知道用C做这件事的最佳方法

我有一个成员函数,需要返回两个自定义类型的列表(
List
),而且我事先知道,我的返回值总是仅为这些列表中的两个

显而易见的选择是:

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

public void returnTwoList(参考列表列表一,参考列表二);
两者似乎都不是最优的

对如何改进这一点有什么建议吗

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

返回:

public class MyTwoLists {
    public List<MyType> ListOne {get;set;}
    public List<MyType> ListTwo {get;set;}
}
公共类MyTwoList{
公共列表列表一{get;set;}
公共列表ListTwo{get;set;}
}

首先,这可能是
输出的
,而不是
ref

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

第三,您可以声明一个泛型
元组
,并返回该元组的一个实例:

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);
类元组{
公共元组(T第一,U第二){
第一=第一;
秒=秒;
}
公共T第一个{get;私有集;}
公共U秒{get;私有集;}
}
静态类元组{
//声明使用以下方法
//编译器类型推断功能,让我们不要指定
//手动输入类型参数。
公共静态元组创建(T第一,U第二){
返回新的元组(第一、第二);
}
}
返回Tuple.Create(firstList,secondList);

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

创建一个简单的结构,同时保存这两个项目并将其作为函数的输出返回?

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

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

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

您有几个选择:

如果列表顺序无意义,则使用一对:

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

public PairJust a余数。NET4.0具有元组类型。我看到您使用了T和U来指定项。如果你有两个以上的元组,你会选择T-U-V-W。。。或者T-U-P-L-E?:)@马克:我没想过。在我考虑之前,我会考虑换成F!
 public Pair<List<MyType>,List<MyType> ReturnTwoLists()
 {
        return new Pair(new List<MyType(), new List<MyType());
 }
 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());
 }