如何从单个函数返回多列表<>值?

AKC*_*AKC 3 c# function list keyvaluepair

尝试从单个函数返回2个List值

我正在使用此代码: -

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);
    }
Run Code Online (Sandbox Code Playgroud)

这里我试图返回2个List<int>值但发生错误.如何从单个函数返回2个列表值

UPDATE

答案是找到的,我们得到了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);
    }
Run Code Online (Sandbox Code Playgroud)

Eni*_*ity 6

在这种情况下,一个相当简洁的方法是使用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!
    */
}
Run Code Online (Sandbox Code Playgroud)