using System; using System.Collections.Generic; namespace MyFirstProgram { class Program { static void Main(string[] args) { // List = data structure that represents a list of objects that can be accessed by index. // Similar to array, but can dynamically increase/decrease in size // using System.Collections.Generic; List food = new List(); food.Add("pizza"); food.Add("hamburger"); food.Add("hotdog"); food.Add("fries"); //Console.WriteLine(food[0]); //Console.WriteLine(food[1]); //Console.WriteLine(food[2]); //Console.WriteLine(food[3]); //food.Remove("fries"); //food.Insert(0, "sushi"); //Console.WriteLine(food.Count); //Console.WriteLine(food.IndexOf("pizza")); //Console.WriteLine(food.LastIndexOf("fries")); //Console.WriteLine(food.Contains("pizza")); //food.Sort(); //food.Reverse(); //food.Clear(); //String[] foodArray = food.ToArray(); foreach (String item in food) { Console.WriteLine(item); } Console.ReadKey(); } } }
I am having some difficulty with being able to manually entering the items into my list through a loop. The problem is specifically the formatting of the 2 foreach loops (same problem with both, and problems with my new List equation foreach(String firearms in MyCollection[]) { Console.WriteLine(firearms); } (for this I am getting CS0443 -Syntax error, value expected/CS0030 - Cannot convert type 'char' to 'string' and CS0136 - A local or parameter named 'firearms cannot be declared in this scope because that name is used in enclosing local scope to define a local or parameter also have a problem with this statement - firearms = Console.ReadLine().ToString(MyCollection[]); Console.WriteLine(MyCollection[]);
@@chrissauter7324 I know this is late but to address your issues: Just remove square brackets "[]" from all your code. You will only use this when references a numerical index for your list, similar to an array.
thanks man, i wish i had found this comment before because I just wanted to refresh my memory on lists and the video was kinda slow, this would've been sm faster 😂😂
I'm 90% sure I tried in the past to learn and understand Arrays and failed hard, even with friends trying to explain, and even tho this is a tutorial for lists, thank you for teaching me what I wanted to do with Arrays in less than a minute xD
Lemme make sure I understand this right: Difference between arrays and lists illustrated with a game example: Say you are making a game with a food and inventory system. The game has a fixed amount of food items/recipes, so they are hardcoded into the array before running the game. This means that the player cannot make "new" food that isnt in the game. If you want to add more recipes into the game later in a new update, you need to update the array. Likewise, a player might have an inventory where they can store their food, whether they made it or crafted it. The inventory has a "list" of items inside of it that can increase or decrease by gathering/cooking new food, or by eating. This means that the player can influence the size of the list in-game, so the list is dynamic. Is this correct? Say you collect an item in a game, after collecting it you can call a "collect();" method (which say you already made) that performs a "food.Add();" command
On behalf of Lists everywhere, I take exception, sir, to your characterization of Lists as "wasting" more memory. It is true that Lists use more memory than arrays; but we'll have you know that those memory resources are very well spent!
"Those who stand at the top determine what's wrong and what's right! This very place is neutral ground! Justice will prevail, you say? But of course it will! Whoever wins this war becomes justice!" ~ Don Quixote Doflamingo
can we add custom Lists like arrays with for loop like List.Add([ i ])=Console.ReadLine(); Can we do something like that for Lists? or are they only added manually for Website application
How do you change number 3 to 0 in list? example: list numbers = new list {1,2,3,4,5,6,7,8,9}; Number 3 needs to be number 0. Is there any way to do some list.Replace() method or anything? Thank you.
Is there any benefit to this over doing something like: string[] food = new string[] { "pizza", "hamburger", "hotdog", "fries"}; and then using '(0, food.Length);', which seems to circumvent the need to declare a definite array size?
I don't quiete have an answer for you but my teacher which owns his own tech company says that you almost never use arrays since lists are much friendlier for continuous development. The only time to use arrays is for things which are actually 100% certain, like there are only 12 months in a year, you don't need a list for that.
using System;
using System.Collections.Generic;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// List = data structure that represents a list of objects that can be accessed by index.
// Similar to array, but can dynamically increase/decrease in size
// using System.Collections.Generic;
List food = new List();
food.Add("pizza");
food.Add("hamburger");
food.Add("hotdog");
food.Add("fries");
//Console.WriteLine(food[0]);
//Console.WriteLine(food[1]);
//Console.WriteLine(food[2]);
//Console.WriteLine(food[3]);
//food.Remove("fries");
//food.Insert(0, "sushi");
//Console.WriteLine(food.Count);
//Console.WriteLine(food.IndexOf("pizza"));
//Console.WriteLine(food.LastIndexOf("fries"));
//Console.WriteLine(food.Contains("pizza"));
//food.Sort();
//food.Reverse();
//food.Clear();
//String[] foodArray = food.ToArray();
foreach (String item in food)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Never stop making vids bro plzzz keep going ❤️❤️❤️
I am having some difficulty with being able to manually entering the items into my list through a loop. The problem is specifically the formatting of the 2 foreach loops (same problem with both, and problems with my new List equation
foreach(String firearms in MyCollection[])
{
Console.WriteLine(firearms);
}
(for this I am getting CS0443 -Syntax error, value expected/CS0030 - Cannot convert type 'char' to 'string' and CS0136 - A local or parameter named 'firearms cannot be declared in this scope because that name is used in enclosing local scope to define a local or parameter
also have a problem with this statement -
firearms = Console.ReadLine().ToString(MyCollection[]);
Console.WriteLine(MyCollection[]);
@@chrissauter7324 I know this is late but to address your issues:
Just remove square brackets "[]" from all your code. You will only use this when references a numerical index for your list, similar to an array.
Summarize :
List is an advanced version of array with many useful methods but it consumes more memory than array .
Method of list
Declare a list
List strList = new List();
// style 1
List strList = new();
// style 2 (quicker)
Add/Insert/Remove an element
Add an element
strList.Add("a");
Insert an element
strList.Insert(1, "b");
// 1 = index
Remove an element
strList.Remove("a");
Size of a list
strList.Count()
Index of a specific element
First index
strList.IndexOf("a");
Last index
strList.LastIndexOf("a");
Check whether a list contains a specific element
strList.Contains("a");
Sort the list
Sort the list alphabetically
strList.Sort();
Sort the list anti-alphabetically
strList.Reverse();
Clear the list
strList.Clear();
Switch between array and list
Convert from array to list
List strList = strArray.ToList();
// style 1
List strList = new();
strList.AddRange(strArray);
// style 2
Convert from list to array
String[] strArray = strList.ToArray();
If you want to print list for testing .
static void Print(List strList)
{
foreach (String str in strList)
{
Console.Write(str);
}
Console.WriteLine();
}
static void Print(List intList) // method overloading
{
foreach (int i in intList)
{
Console.Write(i);
}
Console.WriteLine();
}
thanks man, i wish i had found this comment before because I just wanted to refresh my memory on lists and the video was kinda slow, this would've been sm faster 😂😂
🥳
👍
tysm !!!
I'm 90% sure I tried in the past to learn and understand Arrays and failed hard, even with friends trying to explain, and even tho this is a tutorial for lists, thank you for teaching me what I wanted to do with Arrays in less than a minute xD
I literally never knew the difference between list and array until the 0:18 second of this. Thank you!
Man you sure know how to make a good, quick and easy to understand video.
ONE OF THE BEST TEACHERS OF COMPLEX THINGS 🤔🙌
Thank you bin looking at C# lists vid but yours is the first that is clear enough to me.. really helpful thanks..
Amazing tutorial. You have NO IDEA how much you helped me just now. Thanks so much!!! I have Lists sorted in my brain from now on. :)
The best explanation and thankfully for VDO.
Best explanation so far on UA-cam.
best coding tutorial youtuber ngl
amazing video, great teaching man
Nice tutorial
Thanks
This cleared everything about lists , Thanks
you are the best 🔥🔥🔥🔥🔥
Thanks! Everything very good explained
Im writing an exam in 2 hours and i understand lists better than the 2 months in class. Thank you
good explanation
Descriptive ,simple , concentrate and short video
Watched your video and instantly subscribed! A great Content ❤️💯⭐
Thank you. These short refresher videos are good.
awesome, brooooooo!!!! thanks a lot
W channel. Tq very much sir!
very helpful and easy to understand :D
thanks for making this tutorial
Dude you're a life saver im not joking.
Thanks for the video Bro.
Nice clases Bro!
Very awesome .. thank you!!
Thanks this is going to help me a lot
You're the goat Mr Bro !
Best channel ever
very clear and helpful thankyou
This video helped me so much, thanks
youre so good at explaining things oh my god
Thanks to your efforts :D
your type speed is insane
Lemme make sure I understand this right:
Difference between arrays and lists illustrated with a game example:
Say you are making a game with a food and inventory system. The game has a fixed amount of food items/recipes, so they are hardcoded into the array before running the game. This means that the player cannot make "new" food that isnt in the game. If you want to add more recipes into the game later in a new update, you need to update the array.
Likewise, a player might have an inventory where they can store their food, whether they made it or crafted it. The inventory has a "list" of items inside of it that can increase or decrease by gathering/cooking new food, or by eating. This means that the player can influence the size of the list in-game, so the list is dynamic.
Is this correct? Say you collect an item in a game, after collecting it you can call a "collect();" method (which say you already made) that performs a "food.Add();" command
On behalf of Lists everywhere, I take exception, sir, to your characterization of Lists as "wasting" more memory. It is true that Lists use more memory than arrays; but we'll have you know that those memory resources are very well spent!
Whenever there's a list of objects the objects always gotta be a food of sorts :) great video man.
you are such a bro, bro.
thanks for going straight to the point, nice tutorial
Another great video my man.
I was trying to move from js to C# ,array in C# was feeling so uncomfortable for me. This video made me clear how do i create array as js in C#.
Love it!!!! Thanks for everything
Perfect! Thank you!
yeah brou, your the best, gigachad
short and informative. I really love this.
👍👍
"Those who stand at the top determine what's wrong and what's right! This very place is neutral ground! Justice will prevail, you say? But of course it will! Whoever wins this war becomes justice!"
~ Don Quixote Doflamingo
the best of the best!!
Very well explained
Thank you. Helped me!
Thanks bro
Thanks Bro!
How do I get the average value? I want to use the
Variable.Average(); Command and then assign this average to another variable to display.
can we add custom Lists like arrays with for loop like List.Add([ i ])=Console.ReadLine();
Can we do something like that for Lists? or are they only added manually for Website application
This list method can be used to make a todo list, right?
How do you change number 3 to 0 in list?
example:
list numbers = new list {1,2,3,4,5,6,7,8,9};
Number 3 needs to be number 0.
Is there any way to do some list.Replace() method or anything?
Thank you.
What if you use a
"
numbers.remove(3);
numbers.insert(2, 0);
"
?
numbers[2] = 0;
Thanks
Shot bro.
05:43 what is reverse for?
It’s sorts it alphabetically, but in reverse.
so you could use this to create an inventory system for a video game basically ?
hello same name person just spelled differently.
OMG thank you ! :)
Is there any benefit to this over doing something like:
string[] food = new string[] { "pizza", "hamburger", "hotdog", "fries"};
and then using '(0, food.Length);', which seems to circumvent the need to declare a definite array size?
I don't quiete have an answer for you but my teacher which owns his own tech company says that you almost never use arrays since lists are much friendlier for continuous development. The only time to use arrays is for things which are actually 100% certain, like there are only 12 months in a year, you don't need a list for that.
what about dynamic arrays?
Thanks man
thancks
yeah but how do you get one of the elements out of the list??
Same way as with an array: "string x = food[0];" for the first item in the list.
@@simontillema5599 I appreciate that a lot, this gave me what I needed to finish a project up. All I needed was to pull random objects from a list.
What if I need to udate the value in index 0?
food[0] = "new value";
how to make multiple list?
❤
lesson check😇
list is so similar to vector in C++
pizza 🍕 😀
Super random comment 9999
now i feel hungry
import random
print(random.randint(1,four twenty sixty nine))
Hi
A random comment down below.
random comment below
random comment
guh
this is a random commment AHHHHHHHHHHHH aHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
adsdadadassa