【发布时间】:2016-03-31 20:05:32
【问题描述】:
试图从单个函数返回 2 个列表值
我正在使用此代码:-
public KeyValuePair<int, int> encrypt(string password)
{
List<int> key = new List<int>();
List<int> code = new List<int>();
/*
do stuff, do some more stuff and go!
*/
return new KeyValuePair<List<int>,List<int>>(key,code);
}
在这里我试图返回 2 个List<int> 值,但发生错误。如何从单个函数返回 2 个列表值
更新
找到了答案,我们得到了 2 个正确答案,这就是为什么我不只选择一个,因为两者都很好用
HadiRj的回答
谜题
回答如果你想使用我的代码,那么这是它的正确版本:-
public KeyValuePair<List<int>, List<int>> encrypt(string password)
{
List<int> key = new List<int>();
List<int> code = new List<int>();
/*
do stuff, do some more stuff and go!
*/
return new KeyValuePair<List<int>,List<int>>(key,code);
}
【问题讨论】:
-
创建一个类,添加任何你想返回的属性,然后返回一个类的实例
-
这是个好主意,但还有其他方法
-
是要返回列表(合并)还是返回列表(单独)
-
也许您可以使用 one
List<>对,而不是像您拥有的一对列表?如果是这样,可以使用List<KeyValuePair<int, int>>或者甚至简单地使用Dictionary<int, int>或SortedDictionary<int, int>。这取决于您要允许的数据。在您的解决方案中,键List<>的计数可能大于或小于值List<>的计数。
标签: c# list function keyvaluepair