4

If I have 2+ lists of int numbers is there a way I can store those lists inside another list to pass it to another class or method? I want to be able to access each list individually, but I want to be able to pass them all together in one batch. What would be the syntax this and also what would be the proper method of accessing each list within the master list. Example, I have these individual lists:

List<int> Nums1= new List<int>(new int[] { 6, 76, 5, 5, 6 });
List<int> Nums2= new List<int>(new int[] { 6, 4, 7, 5, 9 });
List<int> Nums3= new List<int>(new int[] { 22, 11, 5, 4, 6 });

I want to then store these in another list to pass to a method such as:

static public List<int> DoSomething (List<int> Allnums)
{

//My calculations

}

Then I want to be able to store them back into a master list and return them to my main.

7 Answers 7

10

You can use a List<List<int>> type to hold a list of lists.

List<List<int>> allNums = new List<List<int>>();

allNums.Add(Nums1);
allNums.Add(Nums2);
allNums.Add(Nums3);

DoSomething(allNums);

//elsewhere
static public List<int> DoSomething (List<List<int>> Allnums)
{

//My calculations

}

With later versions of C# (I believe C# 6 and onwards) you can use collection initializer notation:

var allNums = new List<List<int>>{ Nums1, Nums2, Nums3 };
Sign up to request clarification or add additional context in comments.

3 Comments

Very nice, I will try that now. Thank you! If I want to pass the master list back do I need the return type to be List<List<int>>?
@Oded is there a way to do it directly in the line of declaration of allNums ?
@ykadaru - yes, these days you can - with a collection initializer. var allNums = new List<List<int>>{ Nums1, Nums2, Nums3 };.
1

This is perfectly possible:

static public void DoSomething (List<List<int>> Allnums)
{

//My calculations

}

Notice that there is no reason to return a List; the argument points to the original list of lists. You'd have to put all the lists in a list first though:

List<List<int>> allLists = new List<List<int>>();
allLists.Add(Nums1);
allLists.Add(Nums2);
allLists.Add(Nums3);

DoSomething(allLists);

Comments

1

It depends, as already given as an awnser, you could use a List<List<int>> . Although, since we are using a OO language, and if the amount of lists of ints would be same every time, I would suggest an object.

Something like this:

public class MyIntListObject
{
    public List<int> Output1 {get;set;}
    public List<int> Output2 {get;set;}
}

to hold your lists. I would suggest it for maintaineablity.

Youre method would turn into something like this:

static public MyIntListObject DoSomething (List<int> Allnums)
{

//My calculations

}

1 Comment

The amount of lists would be static. After I get it to work the other way I will try to implement it in this way. Thank you for your input!
0
static public List<int> DoSomething (List<int> Allnums)
{

//My calculations

}

should be

static public List<int> DoSomething (List<List <int>> Allnums)
{

//My calculations

}

Comments

0

what is wrong with :

Tuple<List<int>,List<int>>

Comments

0
//static public List<int> DoSomething (List<int> Allnums)
DoSomething(Nums1.Concat(Nums2).Concat(Nums3).ToList());

Comments

0

I have a similar scenario to create a list of list of a class says ChildClass.

  public class ChildClass
    {
        public string FieldName { get; set; }
        public string Value { get; set; }
        public string StatusClass { get; set; }
        public string StatusMessage { get; set; }
    }

Creating a list of list of ChildClass is as follows:

 List<List<ChildClass>> o = new List<List<ChildClass>> {
            new List<ChildClass>{
           new ChildClass { FieldName = "Name", Value = "Abc", StatusClass = "alert alert-warning", StatusMessage = "This is Name"},
                new ChildClass { FieldName = "Unit Price", Value = "1000", StatusClass = "", StatusMessage = "This is Price"},
               new ChildClass { FieldName = "Type", Value = "Test Type 1", StatusClass = "", StatusMessage = "This is Type"},
               new ChildClass { FieldName = "Status", Value = "true", StatusClass = "", StatusMessage = "This is Status"},
            },
              new List<ChildClass>{
           new ChildClass { FieldName = "Name", Value = "PQR", StatusClass = "", StatusMessage = "This is Name" },
                new ChildClass { FieldName = "Unit Price", Value = "500", StatusClass = "", StatusMessage = "This is Price" },
               new ChildClass { FieldName = "Type", Value = "Test Type 2", StatusClass = "", StatusMessage = "This is Type" },
               new ChildClass { FieldName = "Status", Value = "True", StatusClass = "alert alert-danger", StatusMessage = "This is Status"},
            }
            };

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.