none
List in a List RRS feed

  • Question

  • Trying to make a list inside a list.  Getting syntax error

    private List<List<string, int>> _lstIngredients = new List<List<string, int>>();

    Tuesday, February 25, 2020 3:05 AM

Answers

  • I don't think you can.

    You can make a List<T> on a custom class,  and the custom class can have a List<T> within it of a custom class or a primitive type. 

    • Marked as answer by old_School Tuesday, February 25, 2020 9:41 PM
    Tuesday, February 25, 2020 4:22 AM

All replies

  • I don't think you can.

    You can make a List<T> on a custom class,  and the custom class can have a List<T> within it of a custom class or a primitive type. 

    • Marked as answer by old_School Tuesday, February 25, 2020 9:41 PM
    Tuesday, February 25, 2020 4:22 AM
  • The error is not because the list is inside a List but because you can't do this 

    <string, int>

    You can do this

    List<List<string>> _lstIngredients = new List<List<string>>();

    of this:

    List<Dictionary<string, int>> _lstIngredients = new List<Dictionary<string, int>>();

    Tuesday, February 25, 2020 4:34 AM
  • A list of tuples is another possibility, rather than a dictionary, if you don't need random access.

             List<List<Tuple<string, int>>> _lstIngredients = new List<List<Tuple<string, int>>>();

    Tuesday, February 25, 2020 5:27 AM
  • Hi,

    Thank you for posting here.

    List <T> has only one parameter, so the inner List is wrong.

    If you need such a data structure, look at the Dictionary <TKey, TValue> Class.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 25, 2020 5:31 AM
  • Trying to make a list inside a list.  Getting syntax error

    private List<List<string, int>> _lstIngredients = new List<List<string, int>>();

    As "Ante Meridian" suggested, a Tuple is another possibility.

    Tuple<T1,T2> Class
    https://docs.microsoft.com/en-us/dotnet/api/system.tuple-2?view=netframework-4.8

    C# tuple types
    https://docs.microsoft.com/en-us/dotnet/csharp/tuples

    Example:

    static void Main(string[] args)
    {
        Tuple<string, Nullable<int>>[] tup =
            { new Tuple<string, Nullable<int>>("One", 1),
              new Tuple<string, Nullable<int>>("Two", 2),
              new Tuple<string, Nullable<int>>("Three", 3),
              new Tuple<string, Nullable<int>>("null", null)};
    
        List<Tuple<string, int?>> ltup1 = tup.ToList();
    
        tup[3] = new Tuple<string, int?>("Zero", 0);
        List<Tuple<string, int?>> ltup2 = tup.ToList();
        
        List<List<Tuple<string, int?>>> lltups = new List<List<Tuple<string, int?>>>();
        lltups.Add(ltup1);
        lltups.Add(ltup2);
    
        foreach(var lt in lltups)
        {
            foreach(var t in lt)
            {
                Console.WriteLine("{0}\t{1}", t.Item1, t.Item2);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
    /*
    One     1
    Two     2
    Three   3
    null
    
    One     1
    Two     2
    Three   3
    Zero    0
    */
    

    - Wayne

    Tuesday, February 25, 2020 6:41 AM
  • I think this will likely be the best fit. Thanks everyone for the feedback. These are all great answers 
    Tuesday, February 25, 2020 9:41 PM