3

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);
    }
  • Create a class, add whatever you want to return as properties and return an instance of the class instead – HadiRj Dec 26 '15 at 05:17
  • thats a good idea but got any other way –  Dec 26 '15 at 05:19
  • Is that you want to return list(combined) or return as list(separate) – SachinS Dec 26 '15 at 06:02
  • Maybe you could use *one* `List<>` of pairs, instead of a pair of lists like you have? If so, maybe use `List>` or even simply `Dictionary` or `SortedDictionary`. It depends on what data you want to allow. In you solution it is possible to have the count of the key `List<>` either greater or less than the count of the value `List<>`. – Jeppe Stig Nielsen Dec 26 '15 at 08:36
  • Possible duplicate of [How can I return multiple values from a function in C#?](http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c) – HadiRj Dec 28 '15 at 06:49

6 Answers6

5

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!
    */
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    [Note](https://msdn.microsoft.com/en-us/library/ms229053(VS.80).aspx): _Avoid using out or reference parameters. Working with members that define out or reference parameters requires that the developer understand pointers, subtle differences between value types and reference types, and initialization differences between out and reference parameters._ – HadiRj Dec 26 '15 at 14:57
  • 1
    @HadiRj - I prefer to avoid them too, but sometimes they are appropriate - I used the words "in this case" to help provide that meaning. – Enigmativity Dec 26 '15 at 22:40
2

Change your function deceleration to

public KeyValuePair<List<int>, List<int>> encrypt(string password)

P.S: I'm not recommending this! Creating new class is better idea to handle your problem

HadiRj
  • 1,015
  • 3
  • 21
  • 41
  • you mean to just change the KeyValuePair to your public KeyValuePair, List> ? –  Dec 26 '15 at 05:56
2

way 1: Tuple:

       public Tuple<List<int>, List<int>> func()
    {
        List<int> key = new List<int>() { 2,34,5};
        List<int> code = new List<int>() { 345,67,7};
        return Tuple.Create<List<int>,List<int>>(key, code);

    }

way 2:

viewmodel

    public class retViewModel
{
    public List<int> key { get; set; }
    public List<int> code { get; set; }
}



public retViewModel func()
        {
            List<int> key = new List<int>() { 2,34,5};
            List<int> code = new List<int>() { 345,67,7};
            retViewModel obj = new retViewModel() { 
            code=code,
            key=key
            };
            return obj;
        }
Psar Tak
  • 682
  • 1
  • 9
  • 27
2

You can always return a List<List<int>>. From what I can see from your code the only reason why you use the KVP is because you know you are going to have two lists returned. Then I would say create another object that you can have the key and the code in it:

 public class EncryptionResult
 {
     public IList<int> Key {get; set;}
     public IList<int> Code {get; set;}
 }

I don't recommend you going with the out/ref solution that some other comments suggest. It is not a good practice use them to return several parameters and they should be avoided. Also if you come to extend/modify that object at any point in time because you require more different data you don't need to change the signature of your interface however you need to modify every method and the caller if you change the parameters needed (including all your tests).

Carlos Torrecillas
  • 4,965
  • 7
  • 38
  • 69
2

Very Simple

[WebMethod]
public static List<Teacher>[] BindData()
{
   List<Teacher> list1 = new List<Teacher>();
   List<Teacher> list2 = new List<Teacher>();
   return new List<Teacher>[] { list1, list2 };
}

Note: Both list will use same class as i used Teacher class in both list.

1
List<int> key;
List<int> code;
static void Main(string[] args)
        {
            key = new List<int>();
            code = new List<int>();
            encrypt("",ref key,ref code);
        }
  public void encrypt(string password, ref List<int> key, ref List<int> code)
        {

            /*
               do stuff, do some more stuff and go!
            */


        }