Search code examples
c#listfunctionkeyvaluepair

How to return Multiple List<> values from a single function?


trying to return 2 List values from a single function

I am using this code:-

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

here I am trying to return 2 List<int> values but error occurs. How to return 2 list values from a single function

UPDATE

the answer is found, we got 2 correct answers thats why i didn't just pick one cause both work great

answer by HadiRj

answer by Enigmativity

and if you want to use my code then, this is the correct version of it:-

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

Solution

  • A fairly neat way to go in this case is to use out parameters.

    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!
        */
    }