Make Lists within a List C#

c# lists
c# multidimensional list
nested list c#
get value from list<object in c#
c# list of lists of strings
2d list c#
how to add list class object inside list object in c#
how to retrieve data from list in c#

Using C#, I want to make arraysNames and arraysData be lists of the previous lists. However, I get this error message:

Using the generic type 'List' requires 1 type arguments.

What should I do to fix it?

Thanks in advance!

public string[] traitNames = { "Happiness", "Respect", "Authority" };
public float[] traitData = { 1, 1, 3 };

public string[] suppliesNames = { "Food", "Iron", "Gold" };
public float[] suppliesData = { 3, 2, 1 };

private List[] arraysNames = { traitNames, suppliesNames };
private List[] arraysData = { traitData, suppliesData };

The List is generic class within System.Collections.Generic namespace so you need to pass a Type when declaring its object

something like this:

string[] traitNames = { "Happiness", "Respect", "Authority" };
float[] traitData = { 1, 1, 3 };

string[] suppliesNames = { "Food", "Iron", "Gold" };
float[] suppliesData = { 3, 2, 1 };

List<string[]> arraysNames = new List<string[]>(){ traitNames, suppliesNames };
List<float[]> arraysData = new List<float[]>(){ traitData, suppliesData };

alternatively you can also use a two dimensional array

string[][] arraysNames = new string[][]{ traitNames, suppliesNames };
float[][] arraysData = new float[][]{ traitData, suppliesData }; 

if you dont want to pass the Type, you can use ArrayList instead of List

ArrayList arraysNames = new ArrayList(){ traitNames, suppliesNames };
ArrayList arraysData = new ArrayList(){ traitData, suppliesData };

MSDN link: here

PS: ArrayList is not a generic type of collection so you need typecasting when retrieving its elements

C# Nested Lists: Create 2D List or Jagged List, C# Nested Lists: Create 2D List or Jagged List. Note: When you create a List whose element type is another List, the first List will contain an internal array of List references. And: The List objects these references point to are separate. Var: You can sometimes simplify this syntax by using the implicit typing Create the Button for starting process. Outer ListView with ItemSoucre of GroupStudents. ItemsWrapGrid - for horizontal initialize. Or you can use GridView. DataType of outer list. Inner ListView with Header property and ItemSource StudentItems, which was included like the property of GroupStudent list. DataType of inner list.


You need to provide a type for List<T> since T is a type, just like: string, int, object, string[], int[], object[]...

So, you can try again with:

private List<string[]> arraysNames = { traitNames, suppliesNames };
private List<int[]> arraysData = { traitData, suppliesData };

C# List Examples, Here we create 2 separate lists of ints. We add 4 prime numbers to each List. The values are stored in the order added—2, 3, 5 and then 7. Version 1� Creating a List. AuthorList.Add( "Mahesh Chand" ); AuthorList.Add( "Praveen Kumar" ); AuthorList.Add( "Raj Kumar" ); AuthorList.Add( "Nipun Tomar" ); AuthorList.Add( "Dinesh Beniwal" );


You need to define the type of the List:

This is how you would define a list:

private List<string> arraysData = new List<string>();

So if you want to have an List of Arrays:

List<string[]> arraysNames = new List<string[]>(){ traitNames, suppliesNames };
List<float[]> arraysData = new List<float[]>(){ traitData, suppliesData };

C# List - How to C# List - CSharp, List class is a collection and defined in the System. It's the replacement for arrays, linked lists, queues, and most other one-dimensional all elements in the List after the insertion point have to be shifted to make a free slot. The List generic type in C# provides an underlying and internal array where it stores its elements. But it always specifies a one-dimensional and zero-based array in its internal structure.List. Note: When you create a List whose element type is another List, the first List will contain an internal array of List references.


You want a list of arrays. This is how you initialize them:

private List<string[]> arraysNames =  new List<string[]>()
{ 
    new string[] { "Happiness", "Respect", "Authority" }, 
    new string[] { "Food", "Iron", "Gold" },
};
private List<float[]> arraysData = new List<float[]>()
{ 
    new float[] { 1, 1, 3 },
    new float[] { 3, 2, 1 }
};

Of course, the better design would be to create custom types to hold the data

public class Trait
{
    public Trait(string name) { this.Name = name; }
    public string Name { get; }
    public float Data { get; set; }
}

public class Supplies
{
    public Supplies(string name) { this.Name = name; }
    public string Name { get; }
    public float Data { get; set; }
}

and elsewhere in the code

private List<Trait> traits = new List<Trait>()
{
    new Trait("Happiness") { Data = 1 },
    new Trait("Respect") { Data = 1 },
    new Trait("Authority") { Data = 3 },
};

private List<Trait> supplies = new List<Supplies>()
{
    new Trait("Food") { Data = 3 },
    new Trait("Iron") { Data = 2 },
    new Trait("Gold") { Data = 1 },
};

and even better design improvement would be to use a Dictionary<string, float> to hold the data. For example:

private Dictionary<string,float> traits = new Dictionary<string,float>()
{
    ["Food"] = 3f,
    ["Iron"] = 2f,
    ["Gold"] = 1f,
}

to be used as float gold = traits["Gold"];

C# List<T> Collection, So use IList<T> type variable to create an object of List<T> . However, List<T> includes more helper methods than IList<T> interface. The following table lists the� C# program that copies array to List using System; using System.Collections.Generic; class Program { static void Main () { // Create new array with 3 elements. int [] array = new int [] { 2, 3, 5 }; // Copy the array to a List. List<int> copied = new List<int> (array); // Print size of List.


Lists, Initializing a list with items. In the above example, we just created a list and then we added an item to it. However, C# actually allows you to create a� My question is pretty simple, how can I do this in one statement so that instead of having to new the empty list and then aggregate in the next line, that I can have a single linq statement that outputs my final list. details is a list of items that each contain a list of residences, I just want all of the residences in a flat list.


List<T> Class, Provides methods to search, sort, and manipulate lists. C# Copy. public class List<T> : System.Collections.Generic.ICollection<T>, System The following example demonstrates how to add, remove, and insert a simple business object in a List<T>. public class Example { public static void Main() { // Create a list of parts. Also, if you'd like you can just use a list nested in a list like List<List<string>>, though Stanley's answer is better in terms of design. If you need to use arrays and lists, his List<string[]> solution is the way to go. – Jeremy Kato Jun 29 '16 at 14:02


Creating Python Lists, The values in a list do not need to be unique (the same value can be repeated). Empty lists do not contain any values within the square brackets. C# - List<T> The List<T> collection is the same as an ArrayList except that List<T> is a generic collection whereas the ArrayList is a non-generic collection. The following diagram illustrates the List<T> hierarchy. As shown in the above diagram, the List<T> class implements