简体   繁体   English

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

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

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>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#。

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 Tuple and 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. 您可以针对不同数量的项目扩展此想法。

Return this: 归还这个:

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

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

You have a few options: 你有几个选择:

use a Pair if the lists are meaningless in order: 如果列表按顺序无意义,请使用一对:

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

You can use out or ref parameters, as you mentioned. 如上所述,您可以使用outref参数。 This is a good option if one list is more meaningful than the other. 如果一个列表比另一个列表更有意义,这是一个很好的选择。

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. 这为消费者提供了强类型,良好的静态编译(读取:intellisense-enabled)返回值。 The class can be nested right next to the method. 该类可以嵌套在该方法旁边。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查两个List的最佳方法是什么 <T> C#中的相等列表 - What is the best way to check two List<T> lists for equality in C# 查找不在两个列表中的值的最佳方法C# - Best way to find values not in two lists c# 在C#中比较2个整数列表/数组的最佳方法是什么? - What is the best way to compare 2 integer lists / array in C# C#比较双精度值和一系列值并返回两个文本值的最佳方法是什么 - C# What is the best way to compare a double value to a range of values and return two text values 转换VB if语句将字符串返回到C#的最佳方法是什么 - What is the best way to convert VB if statement that return a string to C# 在C#中,将字符串与null进行比较以及“”返回true的最佳方法是什么 - In C#, what is the best way to compare strings with null and “” return true 比较 c# 中两个对象的最佳方法是什么 - What is the best way to compare two objects in c# 在c#中将两个uint组合成ulong的最佳方法是什么? - What is the best way to combine two uints into a ulong in c# 在C#中比较复杂对象的两个列表的最佳方法是什么 - What is the best way to compare two list of complex object in c# C#在递归返回中添加两个列表 - c# adding two lists in recursive return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM