Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# list<string, list<string>> how to?

Tags:

c#

I've spent some time on the internet trying to find a solution, but my skills in C# are at a beginner level and I did not find any ways to do what i wanted to.

So here is the situation :

I have a web service that return an object to me, in this object there is a two dimensional table. What I'd like to do is running along this table and put these value inside a List. To illustrate that here is an example and what I already have :

// creation of a list like : List <String, List<String>> myList
for (int i = 0; i < outPut.errors.Length; i++)
{
    string error = outPut.errors[i].data.label;
    //myList.add(error);
    if (outPut.errors[i].data.label != "" && outPut.errors[i] != null)
    {
         for (int j = 0; j < outPut.errors[i].data.corrections.Length; j++)
         {
              string corrections  = outPut.errors[j].data.corrections[0][j].ToString();
              //myList.add(corrections)
         }  
    }
}

So in this example i create a two dimension List where the first argument is a String and the second is a List (just like a two dimensional table would have tab[x][y] where there is multiple Y for one X.

Apparently in C# you have to know the exact size to create a two dimensional table, and we will never know what will be the size of this table.

So is it possible to create a list such as i'd like to do it or should i do it another way?

like image 742
Slayner Avatar asked Nov 19 '15 13:11

Slayner


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why do we write C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.


Video Answer


2 Answers

To create a 2 dimensional list use List<List<string>> to define a list where each entry is a list containing strings. You will need to create each "row" list as you come to it, they will not be automatically created for you.

However that is not really what you want in this case as you have the special label field. In this case you should create an object with your string label and list inside it and then have a list of that. Or alternatively if you expect to be looking things up by their label and will not have duplicate labels you could consider a Dictionary<string, List<string>>

like image 62
Tim B Avatar answered Oct 18 '22 22:10

Tim B


This can be achieved with the use of a Tuple as follows:

List<Tuple<String, List<String>>>

Have a look at the details of Tuple here https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx Basically it is a class for storing tuples, pretty much what you are after. The first item is a String, and the second is a List

like image 3
Don Avatar answered Oct 18 '22 22:10

Don