c# - 如何从单个函数返回多个 List<> 值?

标签 c# list function keyvaluepair

尝试从单个函数返回 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 的回答

通过Enigmativity回答

如果你想使用我的代码,那么这是它的正确版本:-

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

最佳答案

在这种情况下,一个相当简洁的方法是使用 out 参数。

public void encrypt(string password, out List<int> key, out List<int> code)
{
    key = new List<int>();
    code = new List<int>();
    /*
       do stuff, do some more stuff and go!
    */
}

关于c# - 如何从单个函数返回多个 List<> 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468691/

相关文章:

c# - WPF 控件,节省空间

c# - 如何创建 Asp.net mvc 安全登录页面身份验证?

C# Foreach 复选框在面板中选中

r - 对存储在列表中的一系列模型进行方差分析

javascript - 为什么这个内部函数返回未定义?

c - 传递 ‘function_name’ 的参数 1 使指针来自整数而不进行强制转换

c# - 从 SqlDataReader 读取字符串时内存不足

Python:如何从字典中删除元素并将其作为列表返回?

list - 如何创建具有特定类型的 List 的包装器

无法使用信号量的指针访问信号量(我认为我使用的是错误的)