In this video, we're going to see an alternative way to initialize a list with data directly.
So in the last videos we just created this list.
And then we added items after the fact.
But if you hover over colors here, you see that the IDE isn't super happy.
So it says collection initialization can be simplified.
So you can click on show potential fixes and then it says collection initialization can be simplified.
So let's hover over this first point and it shows us what a list could look like.
So adding items to the list could work differently.
Like so like the new green area is how it says the new line of code will look like.
And the red stuff though, so the red background code will be deleted.
So let's just click on this and you can see that it automatically changed our list of string colors
to equal.
And then in square brackets.
That's where we are adding items to our list.
And then we finish it with a semicolon.
And each item that we're adding is separated by a comma.
So we're adding red comma.
We're adding blue comma green comma red comma.
So now this way we can go ahead and initialize a list with a few items straight away without having
to use the add keyword all the time.
Now obviously this is just if you already know straight from the get go, so to speak, which items
you want to have in the list directly.
But if you don't know that, or if it's something that comes up later on in the code, then it's totally
fine to use the add method.
This is just if you really already know what you want to put in there, and you can just put it in there.
But luckily this doesn't limit us to add more items to our colors list later on.
So later down the code we could still say add, and then we could still add another blue in here, or
purple or magenta or whatever.
Okay, so this still works.
And now we just add another blue or whatever color we want to use.
So this is just for your awareness.
This is nothing that you need to like.
You don't necessarily need to use this type of code or like to use it this way, but it's just good
practice if you already know exactly what will be there after initialization, basically.
Okay, so if you ever see a list created like this, now you know.
Okay.
So that's it for this video.
See you in the next one.