'how to create list of lists in c#
I am trying to create list of lists, where the large list represent paper contain collection of small list represent question, list of question consist of question string and its ID. here my code:
public class Genes
{
public string question { get; set; }
public int CLO { get; set; }
}
List<Genes> questiongene = new List<Genes>();
List<List<questiongene>> paper = new List<List<questiongene>>();
now I make question list without error but when I try to create largeer list, visual studio can't recognize variable questiontype as a type, where is the wrong ?
Solution 1:[1]
I would do this, rather than a basic List<List<Genes>>
as has been suggested, as this is then clear what each item is...
//This is your class for a single question. A list of these will
//make up a paper.
public class Genes
{
public string Question{ get; set; }
public int CLO { get; set; }
}
//This is a Paper, which contains a list of Questions.
public class Paper
{
public List<Genes> Questions { get; set; }
}
..
..
//To use: First create a list of papers -
//this is a stack of papers.
List<Paper> stackOfPapers = new List<Paper>();
//now create a list of questions which we can add to a paper
List<Genes> questions = new List<Genes>();
//Now we need to create a question to add to the list of questions.
Genes newQuestion = new Genes();
newQuestion.Question = "How many roads must a man walk down?";
newQuestion.CLO = 42;
//now add the question to the list.
questions.Add(newQuestion);
//now we need to create a paper. This will represent one
//paper in our stack of papers.
Paper newPaper = new Paper();
//add our list of Questions to the paper.
newPaper.Questions = questions;
//and finally, add the paper to the stack of papers.
stackOfPapers.Add(newPaper);
//or alternatively, you can use the object initializer syntax to
//do this all in one. Note I've also added more than one paper to
//the list of papers, and each paper has two questions contained in it:
var newStackOfPapers =
new List<Paper>
{
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "How many roads must a man walk down?",
CLO = 42
},
new Genes
{
Question = "Another Question?",
CLO = 111
}
}
},
//add Another paper...
new Paper
{
Questions = new List<Genes> {
new Genes
{
Question = "This is the first question on the second paper?",
CLO = 22
},
new Genes
{
Question = "Another Question?",
CLO = 33
}
}
},
};
Solution 2:[2]
There isn't type questiongene in your code, only type of Genes. Create either a List of Lists:
List<List<Genes>> paper = new List<List<Genes>>();
or create a new type Paper that has the List questiongene:
public class Paper
{
public List<Genes> questionGenes { get; set; }
// default constructor
public Paper()
{
questionGenes = new List<Genes>();
}
// constructor that creates a paper from a List<Genes>
public Paper(List<Genes> questionGene)
{
questionGenes = questionGene;
}
}
and then create a list of Paper:
List<Paper> papers = new List<Paper>();
papers.Add(new Paper(questiongene));
Solution 3:[3]
List<List<string>> myList = new List<List<string>>();
myList.Add(new List<string> { "a", "b" });
myList.Add(new List<string> { "c", "d", "e" });
myList.Add(new List<string> { "qwerty", "asdf", "zxcv" });
myList.Add(new List<string> { "a", "b" });
// To iterate over it.
foreach (List<string> subList in myList)
{
foreach (string item in subList)
{
Console.WriteLine(item);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | thorator |
Solution 3 | Laviz Pandey |