-3

I was wondering if there was a method in which I can hold a generic list that can hold a generic list.

My issue is that I have a list that can hold either LIST A or LIST B or LIST C.

I figured out how to do this with Data types but I want this list to be able to hold classes that I create.

For example:

List<T> listA = new List<T>();

Where T is ObjectX

listA.Add(new list<Y> { new List<U>() { new List<T>() } } );

Where Y is ObjectY<br>

Where U is ObjectU

etc.

EDIT: Let me put it into context.

I have a list of Objects called Suites Each Suite can have a list of CaseObjects OR a list of CaseHolderObjects. Each CaseHolder can hold a list of CaseObjects Each Case can hold a list of ActionObjects

A.Mills
  • 357
  • 3
  • 16
  • 2
    You should use an interface. Have a look at http://stackoverflow.com/questions/1228173/c-sharp-newbie-listinterface-question – Fruchtzwerg Mar 21 '17 at 13:32
  • I think you might want something like `List>` where `BaseClass` is either an abstract class or interface. I'd suggest doing a bit more research about object oriented programming and inheritance. – PJvG Mar 21 '17 at 13:36
  • how deep do you want to go with your nested lists? do you really intend to have a `List>>>` ? or did I miscount?=! – Mong Zhu Mar 21 '17 at 13:45
  • 1
    When `T` is of type `ObjectX`, you can´t put elements of `List` into a `List`. Or is `ObjectX` actually `List`? – MakePeaceGreatAgain Mar 21 '17 at 13:50

4 Answers4

1

I think this is what you want:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace TestList
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Use this syntax for a list of list of classes
            List<List<Test>> test = new List<List<Test>>();
        }
    }
    //This class is just for example.
    class Test
    {
        //Your class code here
    }
}
Rafael
  • 727
  • 13
  • 30
  • Can you explain why? And also what the `Test` class is supposed to represent? – PJvG Mar 21 '17 at 13:40
  • 1
    The test class represents anything, the idea is that you can just create a list of list of classes just by following the syntax: List> test = new List>(); – Rafael Mar 21 '17 at 13:40
  • but the problem with this is once you represent Test, its locked down to what you represented. I want in your case, 'Test' class to be dynamic. List> test = new List>() – A.Mills Mar 21 '17 at 14:24
0

This is what you wanted, a list that holds any other type of list ^^

class Program
{
    static void Main(string[] args)
    {
        Person p1 = new Person(1, "Mario");
        Person p2 = new Person(2, "Franco");
        Dog d1 = new Dog(1, "Fuffy");
        Dog d2 = new Dog(2, "Chop");
        List<Person> listP = new List<Person>();
        listP.Add(p1);
        listP.Add(p2);
        List<Dog> listD = new List<Dog>();
        listD.Add(d1);
        listD.Add(d2);
        List<Object> listO = new List<Object>();
        listO.Add(listP);
        listO.Add(listD);
    }

    public class Person
    {
        public Person(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public int id { get; set; }
        public string name { get; set; }
    }

    public class Dog
    {
        public Dog(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public int id { get; set; }
        public string name { get; set; }
    }

}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • in OP's example it shows a 4-fold nested list. your answer cannot accommodate this. Only the first level works `listO`, but `listP` cannot contain a `List` (I did not downvote) – Mong Zhu Mar 21 '17 at 13:50
  • I suppose that he can do other List ^^ the arraylist example isn't doing what he asked, he wanted to do it with lists – Marco Salerno Mar 21 '17 at 13:51
  • Personally, I would say using `Object` is too generic and it's better to make a new abstract class or interface instead. – PJvG Mar 21 '17 at 13:56
  • you basically need only the`List` to nest 4 lists inside it. But then may be you should show also how to retrieve the data. The arraylist example allows exactly the nesting of lists irrespective of the level. Up to now in your example you show how to go only 2 levels deep – Mong Zhu Mar 21 '17 at 13:57
0

I solved the issue. The classes in question I had them implement an empty interface. I then created a single property.

List<List<Interfaces.IMetaData>> myList = new List<List<Interfaces.IMetaData>>();
            myList.Add(new List<Interfaces.IMetaData>() { new SuiteObject() { } });
            myList.Add(new List<Interfaces.IMetaData>() { new CaseHolderObject() { } });
            myList.Add(new List<Interfaces.IMetaData>() { new CaseObject() { } });
            myList.Add(new List<Interfaces.IMetaData>() { new ActionObject() { } });
A.Mills
  • 357
  • 3
  • 16
-1

You don't need generic collection if you want to store different class in it. Try to use ArrayList (System.Collections namespace). You can add to it any object (of cource cost of performace); For example:

ArrayList listA = new ArrayList() { 1, true, "string" };
ArrayList ListB = new ArrayList() { 2, false };
ArrayList ListC = new ArrayList() { 3, "string3" };

ListB.Add(ListC);
listA.Add(ListB);
daniell89
  • 1,832
  • 16
  • 28