C
C#9mo ago
Blane

How do I add strings to a list?

So what I basically want to do is something along the lines of: Title.Add(Console.ReadLine()); I want to save string values and then display them in the program if the user chooses to option. My problem lies in that I have no idea on how this works. I've looked at the MSDN and YouTube videos but I just can't seem to grasp it. Also, I've built up a menu using switch and cases. The biggest problem of them all is that I have yet to find a good explanation on the difference between List<string[]> Title = new List<string[]>(); and List<string> Title = new List<string>(); Since this is assignment, I don't want the answer, I just want to understand how to actually use them and the difference...
98 Replies
SinFluxx
SinFluxx9mo ago
the difference between the two is the same as the difference between string and string[]
Angius
Angius9mo ago
List<string[]> is a list of arrays of strings List<string> is a list of strings
[
["abc", "def"],
["ghi", "jkl"],
]
[
["abc", "def"],
["ghi", "jkl"],
]
vs
["abc", "def", "ghi"]
["abc", "def", "ghi"]
oke
oke9mo ago
the way i think of it is a string[] is an array, and a List<string> is a string[] with more overhead (not correct at all, but might help with a rough visualization)
Angius
Angius9mo ago
You're right, not correct. True that a list has more overhead than an array, but it also has more functionality. It's dynamically-sized for once So it's not like it's overhead for no reason
oke
oke9mo ago
im just trying not to overwhelm them with a full background of a linked list
Angius
Angius9mo ago
List<T> is not a linked list
oke
oke9mo ago
wait, theres a type for that nm
Angius
Angius9mo ago
So, @Blane, does that answer your question?
Blane
BlaneOP9mo ago
Hmm. I think I understand a little better. So the List<string[]> is like a bucket full of lists consisting of strings? While List<string> is just a singular list of string? (Sorry for the late response. Had to go grab food.)
oke
oke9mo ago
yes ["abc", "def", "ghi"] can be assigned to both a string[] and a List<string> but not a List<string[]>
Blane
BlaneOP9mo ago
But is it possible to assign ["abc", "def", "ghi"] to a list, name that list and then assign the list to a List<string[]>?
SinFluxx
SinFluxx9mo ago
You couldn't do that, no, because an array (string[]) is not the same as a List<string>
Blane
BlaneOP9mo ago
So this assignment I'm working on wants me to make a program similar to a blog, where I first type in a title and then the post/description. I'm not sure how to go about this however since I barely knew anything about the difference between lists and lists[] huh... Hmm
oke
oke9mo ago
// Works
List<string> strList = ["abc", "def", "ghi"];

// Wont work
string[] strArray = strList;

// Will work
string[] strArray = strList.ToArray();
// Works
List<string> strList = ["abc", "def", "ghi"];

// Wont work
string[] strArray = strList;

// Will work
string[] strArray = strList.ToArray();
SinFluxx
SinFluxx9mo ago
Why do you need lists if you're just providing a title and a post/description?
Angius
Angius9mo ago
That seems like a case for classes, not lists or arrays
oke
oke9mo ago
i think he meant the variable name
Angius
Angius9mo ago
class Blogpost
{
public string Title { get; set; }
public string Body { get; set; }
public DateTimeOffset CreatedAt { get; set; }
}
class Blogpost
{
public string Title { get; set; }
public string Body { get; set; }
public DateTimeOffset CreatedAt { get; set; }
}
Blane
BlaneOP9mo ago
I'm sorry if I can't describe it properly...
Angius
Angius9mo ago
Then you could have a List<Blogpost> you can .Add() more blogposts to
oke
oke9mo ago
do you mean the name you give the variable? or an actual property?
Blane
BlaneOP9mo ago
Let me read through it again real quick
Angius
Angius9mo ago
Unless we're doing the good ol' "we won't learn classes yet, so use arrays as a a stand-in for objects" with a blogpost being a string[] where [0] is the title and [1] is the body
Blane
BlaneOP9mo ago
I'll use google translate to help me out "The blog should be a list that can save string vectors"
oke
oke9mo ago
oh, so it is List<string[]> woops
Blane
BlaneOP9mo ago
Every individual blogpost should be a string vector and they should contain at least two elements
Angius
Angius9mo ago
Oof, so I was correct, then
oke
oke9mo ago
kudos
Blane
BlaneOP9mo ago
He added in bold text beneath: "NOTE: You may not replace the vector list with a class"
Angius
Angius9mo ago
So, it seems like you would have a List<string[]> that is the blog, and each individual string[] would be a blogpost
oke
oke9mo ago
so your situation would look like:
[
["abc", "def"], // Can only have 2
["ghi", "jkl"],
// More list elements
]
[
["abc", "def"], // Can only have 2
["ghi", "jkl"],
// More list elements
]
as ZZZZZZZZZZZZ said
Angius
Angius9mo ago
Yeah
oke
oke9mo ago
ya know, im just gonna make this more difficult ill leave this for Z bye
Blane
BlaneOP9mo ago
I appreciate you taking the time to help me!
Angius
Angius9mo ago
Or, more aptly,
[
[ "Title", "Lorem Ipsum Dolor Sit Amet" ],
[ "Hamburger", "Hamburgers are the new superfood according to the scientists" ]
]
[
[ "Title", "Lorem Ipsum Dolor Sit Amet" ],
[ "Hamburger", "Hamburgers are the new superfood according to the scientists" ]
]
So, you'd have your
var blog = new List<string[]>();
var blog = new List<string[]>();
and you would be adding blogposts to it with
var post = new[]{ "Title", "Body" };
blog.Add(post);
var post = new[]{ "Title", "Body" };
blog.Add(post);
oke
oke9mo ago
i think you just gave the answer to their assignment...
Angius
Angius9mo ago
I'm not sure how I could've obfuscated this answer lol
oke
oke9mo ago
touche lol
Blane
BlaneOP9mo ago
So var blog = new List<string[]>(); is to make the bucket which I will add the blog post to?
Angius
Angius9mo ago
A bucket of buckets Yes
Blane
BlaneOP9mo ago
Hmmmm.... I think I'm starting to understand this now but there are some gaps
Angius
Angius9mo ago
Yeah, it doesn't make much sense to treat each blogpost as a bucket of strings rather than an object with properties But that's what the requirements are Let's maybe distinguish them. Each post on the blog is a bag of strings And the blog is a bucket of those bags
Blane
BlaneOP9mo ago
So the blog in this case is both the Title + Post? Doesn't help that english isn't my first language
Angius
Angius9mo ago
Yes, the blogpost is a bag that contains the title and the post
Blane
BlaneOP9mo ago
Waaaait.... Hold on
Blane
BlaneOP9mo ago
No description
Blane
BlaneOP9mo ago
The second var post = new[]{ "Title", "Body" }; blog.Add(post); What is the reason for the [] next to new?
Angius
Angius9mo ago
To indicate it's an array
oke
oke9mo ago
its shorthand the full version is:
new string[]
new string[]
it makes writing code faster
Angius
Angius9mo ago
But since both elements are strings, the compiler can figure out that it's strings we want to have there So we don't need to specify it explicitly
Blane
BlaneOP9mo ago
Aaah... So if I were to write it, I would type it out like string post = new string[] {"Title", "Body"};
oke
oke9mo ago
yup
Blane
BlaneOP9mo ago
hmm okay Then it makes more sense Let me think real quick string blog = new List<string[]>(); string post = new string[]{ "Title", "Body" }; blog.Add(post);
oke
oke9mo ago
this is also dependant of which language version youre using. if youre using .net Framework (not .net Core) then you have to manually change the language version so using new[] (or new()) would throw an error
Angius
Angius9mo ago
This would work, yes
Blane
BlaneOP9mo ago
I honestly don't remember which one I'm using
oke
oke9mo ago
string blog = new List<string[]>(); would throw an error because youre assinging a List<string[]> to a string
Angius
Angius9mo ago
No Yeah
string post = new string[]{ "Title", "Body" };
string post = new string[]{ "Title", "Body" };
would not work As oke said
oke
oke9mo ago
thats a separate issue, so still valid
Angius
Angius9mo ago
Neither would
string blog = new List<string[]>();
string blog = new List<string[]>();
oke
oke9mo ago
string myString = "This is text";

string[] myStringVector = new string[] { "This is element one", "Element two" };

List<string> myStringList = new List<string>() { "This is element one", "Element two" };

List<string[]> myStringListVector = new List<string[]>()
{
{ "Title", "Body" },
{ "Title", "Body" }
};
string myString = "This is text";

string[] myStringVector = new string[] { "This is element one", "Element two" };

List<string> myStringList = new List<string>() { "This is element one", "Element two" };

List<string[]> myStringListVector = new List<string[]>()
{
{ "Title", "Body" },
{ "Title", "Body" }
};
Blane
BlaneOP9mo ago
hmm
Blane
BlaneOP9mo ago
So to make sure I get it... This is where I create the "bucket" or where I will give the myStringList its title?
No description
Blane
BlaneOP9mo ago
I think what messes me up is the {} afterwards... Also, I feel really dumb and sorry for taking up so much of your time I'm very slow
oke
oke9mo ago
using {} instead of [] is dependant on how you set the variable im going to use the long version ({}) in case youre using a different language version
Blane
BlaneOP9mo ago
So when I have used lists before, we use [0], [1], [2] and [i] and so on.
oke
oke9mo ago
this is a constant length, and will always be that assortment of values until its changed what you want is this datatype:
List<string[]>
List<string[]>
Angius
Angius9mo ago
You can
var arr = new string[2];
arr[0] = "foo";
arr[1] = "bar";
var arr = new string[2];
arr[0] = "foo";
arr[1] = "bar";
is equivalent to
var arr = new[]{ "foo", "bar" };
var arr = new[]{ "foo", "bar" };
oke
oke9mo ago
List<string[]> myBlogs = new List<string[]>()
{
{ // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
},
{ // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
},
}
List<string[]> myBlogs = new List<string[]>()
{
{ // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
},
{ // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
},
}
Angius
Angius9mo ago
Let's maybe skip some of the syntax sugar
List<string[]> myBlogs = new List<string[]>()
{
new string[] { // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
},
new string[] { // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
},
}
List<string[]> myBlogs = new List<string[]>()
{
new string[] { // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
},
new string[] { // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
},
}
oke
oke9mo ago
oh, forgot to do that with the inner parts too lol
Angius
Angius9mo ago
It's equivalent to
List<string[]> myBlogs = new List<string[]>();

myBlogs.Add(new string[] { // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
});

myBlogs.Add(new string[] { // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
});
List<string[]> myBlogs = new List<string[]>();

myBlogs.Add(new string[] { // accessed by 'myBlogs[0]'
"Title", // accessed by 'myBlogs[0][0]'
"Blog" // accessed by 'myBlogs[0][1]'
});

myBlogs.Add(new string[] { // accessed by 'myBlogs[1]'
"Title", // accessed by 'myBlogs[1][0]'
"Blog" // accessed by 'myBlogs[1][1]'
});
Blane
BlaneOP9mo ago
I think I can make it work if I figure out how to register what the user types as the title and blog. The question is how I do that. I've tried with Console.ReadLine(); but I can't get that to work.
oke
oke9mo ago
Console.ReadLine() prompts the user for text in the console you have to type text, then press enter for the application to get the string
Blane
BlaneOP9mo ago
oooh wait. I accidentally used the vector list instead of the normal one
Angius
Angius9mo ago
There's no "vector" in C#, by the way Just so you know
Blane
BlaneOP9mo ago
okay... so
Angius
Angius9mo ago
List<T> is a list T[] is an array
Blane
BlaneOP9mo ago
Language barrier
oke
oke9mo ago
based on the description, List<string[]> was the right one
Blane
BlaneOP9mo ago
You know what. I'll just send you a screenshot
oke
oke9mo ago
i was under the impression C#s List<> == C++s vector<>
Blane
BlaneOP9mo ago
It's the case 2:
Angius
Angius9mo ago
Yes, but it is not called a vector
oke
oke9mo ago
ohh, okay
Angius
Angius9mo ago
You should create the blogpost array first Then add that array to the list of blogposts
oke
oke9mo ago
i dont think we'll be able to explain this properly without inadvertently answering your assignment for you
Angius
Angius9mo ago
Yeah
Blane
BlaneOP9mo ago
Hmm...That's true okay. Let's see. I now know (at least I think I do) what the difference between the <string[]> and <string> is
Angius
Angius9mo ago
1. Make an array of strings of length 2 2. Read user input into the [0]th element for the title 3. Read user input into the [1]st element for the body 4. Add the array to the list string[] is an array of strings string is string
Blane
BlaneOP9mo ago
Okay. That makes sense
Angius
Angius9mo ago
List<string[]> is a list of arrays of strings List<string> is a list of strings
oke
oke9mo ago
List<string> liknar en shoppinglista där du skriver en vara per rad. Exempel: - Mjölk - Bröd - Ägg - List<string[]> liknar en inköpslista där du grupperar varor i kategorier. Varje kategori kan innehålla flera underposter. Exempel: * Mjölkprodukter: - Mjölk - Fil * Bröd: - Rågbröd - Vetebröd - Använder du List<string> så kan du enkelt lägga till eller ta bort varor på listan. List<string[]> används om du vill ha en mer strukturerad lista med underkategorier.
Blane
BlaneOP9mo ago
aaaaaaah
oke
oke9mo ago
List<string> - Enkel inköpslista Denna kod visar hur man använder en List<string> för att skapa en enkel inköpslista:
// Skapa en lista för inköpsvaror
List<string> inköpslista = new List<string>();

// Lägg till varor i listan
inköpslista.Add("Mjölk");
inköpslista.Add("Bröd");
inköpslista.Add("Ägg");

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string vara in inköpslista)
{
Console.WriteLine(vara);
}
// Skapa en lista för inköpsvaror
List<string> inköpslista = new List<string>();

// Lägg till varor i listan
inköpslista.Add("Mjölk");
inköpslista.Add("Bröd");
inköpslista.Add("Ägg");

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string vara in inköpslista)
{
Console.WriteLine(vara);
}
I detta exempel skapas en lista inköpslista av typen List<string>. Sedan läggs olika varor till listan med metoden Add(). Slutligen loopar vi igenom listan med en foreach-loop och skriver ut varje vara. List<string[]> - Grupperad inköpslista med kategorier Denna kod visar hur man använder List<string[]> för att skapa en inköpslista med kategorier:
// Skapa en lista för kategorier med varor
List<string[]> inköpslista = new List<string[]>();

// Lägg till en kategori med varor
inköpslista.Add(new string[] { "Mjölk", "Fil" });
inköpslista.Add(new string[] { "Rågbröd", "Vetebröd" });

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string[] kategori in inköpslista)
{
Console.WriteLine($" - {kategori[0]}:"); // Skriv ut kategorinamn
foreach (string vara in kategori)
{
Console.WriteLine($" * {vara}"); // Skriv ut varje vara
}
}
// Skapa en lista för kategorier med varor
List<string[]> inköpslista = new List<string[]>();

// Lägg till en kategori med varor
inköpslista.Add(new string[] { "Mjölk", "Fil" });
inköpslista.Add(new string[] { "Rågbröd", "Vetebröd" });

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string[] kategori in inköpslista)
{
Console.WriteLine($" - {kategori[0]}:"); // Skriv ut kategorinamn
foreach (string vara in kategori)
{
Console.WriteLine($" * {vara}"); // Skriv ut varje vara
}
}
Här skapas en lista inköpslista av typen List<string[]>. Varje element i listan är en egen array med strängar, som representerar en kategori med tillhörande varor. Vi lägger till kategorier med hjälp av Add() och en ny array med varunamn. När vi loopar igenom listan går vi först igenom varje kategori (kategori), sedan skriver vi ut kategorinamnet. Inuti den loopen går vi igenom varje vara (vara) i kategorin och skriver ut den.
MODiX
MODiX9mo ago
oke
REPL Result: Success
List<string> inköpslista = new List<string>();

// Lägg till varor i listan
inköpslista.Add("Mjölk");
inköpslista.Add("Bröd");
inköpslista.Add("Ägg");

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string vara in inköpslista)
{
Console.WriteLine(vara);
}
List<string> inköpslista = new List<string>();

// Lägg till varor i listan
inköpslista.Add("Mjölk");
inköpslista.Add("Bröd");
inköpslista.Add("Ägg");

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string vara in inköpslista)
{
Console.WriteLine(vara);
}
Console Output
Inköpslista:
Mjölk
Bröd
Ägg
Inköpslista:
Mjölk
Bröd
Ägg
Compile: 482.941ms | Execution: 41.809ms | React with ❌ to remove this embed.
MODiX
MODiX9mo ago
oke
REPL Result: Success
List<string[]> inköpslista = new List<string[]>();

// Lägg till en kategori med varor
inköpslista.Add(new string[] { "Mjölk", "Fil" });
inköpslista.Add(new string[] { "Rågbröd", "Vetebröd" });

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string[] kategori in inköpslista)
{
Console.WriteLine($" - {kategori[0]}:"); // Skriv ut kategorinamn
foreach (string vara in kategori)
{
Console.WriteLine($" * {vara}"); // Skriv ut varje vara
}
}
List<string[]> inköpslista = new List<string[]>();

// Lägg till en kategori med varor
inköpslista.Add(new string[] { "Mjölk", "Fil" });
inköpslista.Add(new string[] { "Rågbröd", "Vetebröd" });

// Skriv ut innehållet i listan
Console.WriteLine("Inköpslista:");
foreach (string[] kategori in inköpslista)
{
Console.WriteLine($" - {kategori[0]}:"); // Skriv ut kategorinamn
foreach (string vara in kategori)
{
Console.WriteLine($" * {vara}"); // Skriv ut varje vara
}
}
Console Output
Inköpslista:
- Mjölk:
* Mjölk
* Fil
- Rågbröd:
* Rågbröd
* Vetebröd
Inköpslista:
- Mjölk:
* Mjölk
* Fil
- Rågbröd:
* Rågbröd
* Vetebröd
Compile: 467.845ms | Execution: 51.133ms | React with ❌ to remove this embed.
Blane
BlaneOP9mo ago
Thank you guys so much for the help. Was finally able to understand it now after some sleep. I really appreciate you guys taking the time out of your day to help me. The Swedish explanation helped a lot!
Want results from more Discord servers?
Add your server