C# Lists 📃
Вставка
- Опубліковано 2 лип 2021
- C# lists tutorial example explained
#C# #list #tutorial
// 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; - Наука та технологія
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.
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
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 😂😂
🥳
👍
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. :)
Best explanation so far on UA-cam.
Finally understand how to use Lists. Thank you!
Whenever there's a list of objects the objects always gotta be a food of sorts :) great video man.
I literally never knew the difference between list and array until the 0:18 second of this. Thank you!
best coding tutorial youtuber ngl
Perfect! Thank you!
Dude you're a life saver im not joking.
Another great video my man.
thanks for going straight to the point, nice tutorial
amazing video, great teaching man
Thanks! Everything very good explained
very helpful and easy to understand :D
Love it!!!! Thanks for everything
The best explanation and thankfully for VDO.
Thank you. These short refresher videos are good.
you are the best 🔥🔥🔥🔥🔥
This cleared everything about lists , Thanks
Watched your video and instantly subscribed! A great Content ❤️💯⭐
Thanks for the video Bro.
Thanks this is going to help me a lot
Very awesome .. thank you!!
Thank you. Helped me!
You're the goat Mr Bro !
youre so good at explaining things oh my god
very clear and helpful thankyou
Nice clases Bro!
Nice tutorial
Thanks
Thanks Bro!
the best of the best!!
awesome, brooooooo!!!! thanks a lot
Thanks to your efforts :D
W channel. Tq very much sir!
OMG thank you ! :)
Very well explained
Best channel ever
thanks for making this tutorial
yeah brou, your the best, gigachad
Descriptive ,simple , concentrate and short video
This video helped me so much, thanks
you are such a bro, bro.
good explanation
Thanks man
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!
short and informative. I really love this.
your type speed is insane
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#.
👍👍
❤
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
Thanks
Shot bro.
"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
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.
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;
This list method can be used to make a todo list, right?
so you could use this to create an inventory system for a video game basically ?
hello same name person just spelled differently.
thancks
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.
how to make multiple list?
lesson check😇
list is so similar to vector in C++
What if I need to udate the value in index 0?
food[0] = "new value";
what about dynamic arrays?
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.
05:43 what is reverse for?
It’s sorts it alphabetically, but in reverse.
Super random comment 9999
pizza 🍕 😀
import random
print(random.randint(1,four twenty sixty nine))
A random comment down below.
Hi
this is a random commment AHHHHHHHHHHHH aHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
adsdadadassa