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);
    }
5
  • Create a class, add whatever you want to return as properties and return an instance of the class instead
    – HadiRj
    Dec 26, 2015 at 5:17
  • thats a good idea but got any other way
    – user3651521
    Dec 26, 2015 at 5:19
  • Is that you want to return list(combined) or return as list(separate)
    – SachinS
    Dec 26, 2015 at 6:02
  • Maybe you could use one List<> of pairs, instead of a pair of lists like you have? If so, maybe use List<KeyValuePair<int, int>> or even simply Dictionary<int, int> or SortedDictionary<int, int>. 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<>. Dec 26, 2015 at 8:36
  • Possible duplicate of How can I return multiple values from a function in C#?
    – HadiRj
    Dec 28, 2015 at 6:49

6 Answers 6

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!
    */
}
2
  • 1
    Note: 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, 2015 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. Dec 26, 2015 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

1
  • you mean to just change the KeyValuePair<int,int> to your public KeyValuePair<List<int>, List<int>> ?
    – user3651521
    Dec 26, 2015 at 5: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;
        }
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).

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


        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy