Welcome back.
In this video I want to introduce lists to you.
And you're going to learn when to use them and how to use them.
And then we're going to have a bunch of examples where we're going to apply everything that you're going
to learn in this presentation.
So first of all, what is a list?
Well a list, as you can see that it's surrounded by these less than and greater sign brackets.
T is a generic collection that stores elements in a linear sequence.
And unlike arrays, lists are dynamic, which means their size can increase or decrease as needed.
Because we have seen with arrays that we need to define how many items we want to have in that array,
and we cannot change it after we initialized it.
And here we can actually change the amount of items that are in the list.
So what is this t?
Well, the T in the list t in C sharp is a placeholder for a type, and you can think of it like a blank
space and a sequence that you can fill in with any type like int string or even a class you create like
car or our customer that we created earlier.
And this allows you to create a list specifically for that type of item.
So imagine you have a box where you want to store only apples and you could label this box apple box.
Similarly, if you have a box for storing only books, you might label it book Box and in C sharp this
list less than T greater than is like one of these boxes, but you can decide what type of items it's
going to hold when you start using it.
So the T is the label you put on the box, and it tells you what you can put inside the box.
So here we have an example of numbers.
So here's the box that holds numbers only.
So list and less than sign int greater than sign numbers.
And then we create this new list.
And again the less than sign int.
So the data type greater than sign.
And then the brackets and obviously the semi-colon.
And then we're using something called a list method the add method where we add the number one to the
box.
And then we do the same thing with the number two in the next line.
And we're going to see a bunch of methods that lists offer.
Now we have this other box which we call names.
And this is going to be a list of strings.
So you can see the structure is the same.
Only that now it only accepts strings.
And this only works for the base data types or the simple data types.
But it also works for objects of classes that we are creating or that are there already.
So why would you use this T character capital T?
Well, it makes list versatile because you can define what type of items it should hold each time you
use it.
Just like how you can decide what to put in a box each time you get a new box.
This helps you keep your items organized and safe, ensuring that you don't accidentally put the wrong
type of item into your list or box.
So in summary, T in list T is a way to create a list that is custom tailored to hold a specific type
of item, which you can decide when you create the list.
This makes your code flexible and reusable with any data type.
So let's look at the properties of lists.
Lists have the count property that gets the number of elements contained in the list, and then you
have the capacity property which gets or sets the number of elements that the list can hold before needing
to resize.
So let's look at the core methods that we have.
So we have this add item and remove item.
So add item adds an item to the end of the list and remove item removes the first occurrence of a specific
object from the list.
So we're going to see what that means exactly.
As you can see, it's not just removing the last item or the first item, but it removes the actual
item itself that we are well asking to remove.
But we can also be more specific about what we want to remove.
Then there is this insert method int index t item, which means you can insert an element at a specified
index.
And then there is this find method that searches for an element that matches the condition defined by
a specified predicate.
And we're going to see what predicates are and how to use them, or define methods specifically inside
of the our demos.
Right.
Or in the demos.
So then we have the sort method which allows you to sort elements in the entire list, which is really
cool, very powerful feature, very simple.
Then we have the clear method which removes all elements from the list.
And we have this two array method, which allows you to copy the elements of the list to a new array.
So if you want to use a list but you need to use an array, you can convert the list into an array and
then use that array.
Because let's say the method that you're using is requiring an array instead of a list.
Then you can convert it using the two array method.
And then there is the index of item.
This returns the index where this particular item is.
But very important here, the first occurrence of that specific item or object.
So if you have this item multiple times in the list, it will still only give you the first item.
So that's something you need to be aware of.
So what are typical use cases for lists.
Well first of all database records.
So when you fetch records from a database and the number of records is unknown beforehand, a list is
ideal because of its dynamic nature.
Then there is data manipulation, where lists are helpful when you need to add, remove or sort elements
frequently, and then the return types for collections.
So methods that return a collection of items often use lists because they are more flexible and powerful
than arrays.
Okay, so what are best practices.
Well there is the capacity management that you have to be aware of.
So if you know the approximate number of items that the list will hold, setting the initial capacity
can improve performance by reducing the number of memory relocations needed as the list grows.
And then you should use for each loops whenever working with lists when you don't need to modify the
list during the iteration.
So for each loops, just offer a cleaner syntax compared to for loops.
Now let's compare the two.
First of all the size flexibility.
So arrays are fixed in size.
Meaning once you define the number of elements it can hold, it cannot be resized without creating a
new array.
Lists, on the other hand, can adjust its size dynamically.
So whenever you need a dynamic collection, lists are the way to go.
When it comes to performance.
Arrays offer slightly better performance, so for indexing and iteration, because they are simpler
and have direct access to their memory locations.
However, lists are also still quite efficient and they have a slight overhead due to dynamic resizing
and other additional features that run in the background.
Then there's the functionality, and arrays provide a straightforward data structure for storing and
accessing fixed numbers of elements.
However, lists come with many more built in methods for searching, sorting, and manipulating data,
which can come in handy very often.
So when it comes to ease of use, lists are generally easier to use when you need to add or remove items
frequently.
However, arrays are preferable when the number of elements is known in advance and does not change.
Okay, so this has given you a little bit of an overview of arrays and lists how they compare to each
other and in general how lists work.
But obviously we're going to go into a lot of practice as well with some exercises and obviously some
demos.
So see you in the next video.